Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Problema ciclo foreach

  1. #1

    Problema ciclo foreach

    Ciao a tutti,

    sto progettando una forum board insieme ad un mio amico e ho un problema nel visualizzare un mini riassunto delle statistiche di ogni utente in ogni post.

    Questo è il file thread.php che uso
    Codice PHP:
    <?php
    ob_start
    ();
    require_once(
    "./global.php");
    include(
    "./config.php");
    if (
    file_exists($file) == TRUE) {
    $title "Errore";
    } else {
    if (
    $_GET['tid']) {
    $tid intval(mysql_real_escape_string(htmlspecialchars($_GET['tid'])));
    $query_tid_title mysql_query("SELECT title FROM ".$config['prefix']."threads WHERE tid = ".$tid."") or die(mysql_error());
    $fetch_title mysql_fetch_array($query_tid_title);
    $title htmlspecialchars($fetch_title['title']);
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="it" lang="it">

    <head>
    <title><?php echo "".$board['bbname']." - ".$title.""?></title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    </head>
    <body>
    <div id="logo">LOGO</div>


    <?php
    if (file_exists($file) == TRUE) {
    echo 
    "[b]Rinomina/elimina il file install.php prima di usare questa applicazione[/b]";
    } else {
    if (! 
    $_COOKIE['username'] && ! $_COOKIE['password']) {
    echo 
    'Non puoi accedere alle discussioni senza essere loggato';
    header("Location: index.php");
    } else {
    echo 
    "<span style=\"float:right;\"><a href=\"threads.php?action=newreply&tid=$tid\"><img src=\"newreply.png\" /></a></span>
    "
    ;
    $query_first_post_thread mysql_query("SELECT * FROM ".$config['prefix']."threads WHERE tid = ".$tid."") or die(mysql_error());
    $first_post mysql_fetch_array($query_first_post_thread);
    $query_info_user_first_post mysql_query("SELECT * FROM ".$config['prefix']."users WHERE username = '".$first_post[author]."'") or die(mysql_error());
    $info_first_user mysql_fetch_array($query_info_user_first_post);
    echo 
    "<table border=\"1\"><tr><td width=\"30%\"><a href=\"users.php?uid=$info_first_user[uid]\">$info_first_user[username]</a>
    "
    ;
    echo 
    "Messaggi inviati: $info_first_user[message]

    </td>"
    ;
    $messaggio htmlspecialchars($first_post['subject']);
    echo 
    "<td>$messaggio</td></tr>";
    $query_select_all_other_posts mysql_query("SELECT * FROM ".$config['prefix']."posts WHERE tid = '".$tid."'") or die(mysql_error());
    if (
    mysql_num_rows($query_select_all_other_posts)) {
    $all_posts mysql_fetch_array($query_select_all_other_posts);
    foreach(
    $all_posts as $post) {
    $query_author mysql_query("SELECT * FROM ".$config['prefix']."users WHERE username = '".$post[author]."'") or die(mysql_error());
    $author_info mysql_fetch_array($query_author);
    echo 
    "<tr><td width=\"30%\"><a href=\"users.php?uid=$author_info[uid]\">$author_info[username]</a>
    "
    ;
    echo 
    "Messaggi inviati: $author_info[message]
    </td>"
    ;
    $mex htmlspecialchars($post['subject']);
    echo 
    "<td>$post[$subject]</td></tr>";
    }
    }
    }
    echo 
    '</table>';
    echo 
    "
    <span style=\"float:right;\"><a href=\"threads.php?action=newreply&tid=
    $tid\"><img src=\"newreply.png\" /></a></span>
    "
    ;
    }
    if (
    $_GET['action'] == "newreply") {
    if (
    intval($_GET['tid'])) {
    echo 
    '<form action="" method="post"><table border="1"><tr><td colspan="2">Invio nuova risposta</td></tr>';
    echo 
    '<tr><td>[b]Messaggio[/b]:</td><td><textarea cols="50" rows="25" name="oggetto_messaggio"></textarea></td></tr>';
    echo 
    '<tr><td colspan="2"><input type="submit" value="Invia nuova risposta" /></td></tr></table></form>';
    $post_subject mysql_real_escape_string($_POST['oggetto_messaggio']);
    if (! 
    $post_subject) {
    echo 
    '<script>alert("compila i campi richiesti")</script>';
    } else {
    $autore mysql_real_escape_string($_COOKIE['username']);
    $query_new_post mysql_query("INSERT INTO ".$config['prefix']."posts (pid, tid, author, subject) VALUES (NULL, '$tid', '$autore', '$post_subject')") or die(mysql_error());
    $query_update_num_post mysql_query("UPDATE ".$config['prefix']."users SET message = message+1 WHERE username = '".$autore."'") or die(mysql_error());
    header("Location: threads.php?tid=$tid"); 
    }
    }
    }
    }
    ?>
    Purtroppo però il ciclo foreach non funziona bene e mi restituisce un errore:



    In poche parole il primo post viene prelevato direttamente dalla tabella threads mentre gli altri dalla tabella posts
    Come risolvo?
    Grazie mille

  2. #2
    non ho letto per bene perche' le righe dello script son troppo lunghe e poco spaziate e senza rientri ... ergo difficili da leggere. Pero' ho notato una cosa: l'uso di mysql_fetch_array e poi il foreach sul result set. Non puo' andare bene. mysql_fetch_array estrae due volte il record selezionato. Una con indice associativo e l'altra con indice numerico. Meglio se le prove le fai usando mysql_fetch_assoc()

    Tieni presente che ho dovuto usare l'opzione find del browser per trovare le query.... significa che e' uno script veramente poco leggibile per gli esseri umani ... specie se non piu' tanto recenti.

    Il silenzio è spesso la cosa migliore. Pensa ... è gratis.

  3. #3
    Io l'ho creato con notepad++ e questo lo rende così con blocco note o qualche altro programma. Però se lo apri con notepad++ appare normale.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.