This code is not outputting different records for a field

Discussion in 'PHP' started by tyler_durden, Mar 28, 2007.

  1. #1
    I'm not exactly sure what I missed in the code below, but this script outputs all the rows correctly, but the last field marked in red outputs the same record throughout the whole script- the first record it pulls up. Everything else is correct. What gives?
    <?
    function db2me( $time )
    {    
    return date( "M d, Y", strtotime( $time ) );
    }
    if( !($result = mysql_query("SELECT * FROM reminders WHERE usrname='$my->username' ORDER BY date_field ASC ")) ): die("Cannot query mysql server");
    elseif( !mysql_num_rows( $result ) ): 
    printf("No records found for %s", $my->username );
    else: 
    
    
    
    $date_field=mysql_result($result,$i,"date_field");
    $subject=mysql_result($result,$i,"subject");
    $recipient=mysql_result($result,$i,"recipient");
    $recurring=mysql_result($result,$i,"recurring");
    $remid=mysql_result($result,$i,"remid");
    
    
    
    echo "<table border='1'>\n".   
    "<tr>\n".   
    "<td>Date</td>".   
    "<td>Occasion</td>".   
    "<td>Recipient</td>". 
    "<td>Reoccuring</td>".
    "<td>Edit</td>".
    "<td>Delete</td>".  
    "</tr>\n";   
    while( $array = mysql_fetch_assoc( $result ) ):
        echo "<tr>\n";
        printf( "<td>%s</td>\n", db2me( $array['date_field'] ) );
        printf( "<td>%s</td>\n", $array['subject'] );
        printf( "<td>%s</td>\n", $array['recipient'] );
        printf( "<td>%s</td>\n", $array['recurring'] );
        printf( "<td>edit</td>\n" );
        [COLOR="Red"]printf( "<td><a href='delete-reminder.php?remid=$remid'>Delete</a></td>\n" );[/COLOR]
        echo "</tr>\n";
       endwhile;
     echo "</table>";
    endif;
    ?>
    Code (markup):

     
    tyler_durden, Mar 28, 2007 IP
  2. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #2
    I noticed you call all rows in an $array['value'] way. Only remid isn't called like this... So maybe:

    
    print( '<td><a href="delete-reminder.php?remid='.$array['remid'].'">Delete</a></td>\n' );
    
    PHP:
    Just a thought :)
     
    Louis11, Mar 28, 2007 IP
  3. serjio28

    serjio28 Peon

    Messages:
    37
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    There is a wonderful operator - extract. It can help us to reduce amount of code :) . I changed a bit of your code and leave only general operations. Paid attention to the red colored lines. And you shouldn't use printf operator without argument list. I don't know much about this case in PHP but in C/C++ language it may cause several problems.

    <?
    $result = mysql_query("SELECT * FROM reminders WHERE usrname='$my->username' ORDER BY date_field ASC ");
    
    while ( $row = mysql_fetch_assoc ($result) )
    {
        [COLOR="Red"] @extract($row);[/COLOR]
        echo "<tr>\n";
        printf( "<td>%s</td>\n", db2me( $date_field ) );
        printf( "<td>%s</td>\n", $subject );
        printf( "<td>%s</td>\n", $recipient );
        printf( "<td>%s</td>\n", $recurring );
        [COLOR="Red"]print( "<td>edit</td>\n" );[/COLOR]
        [COLOR="Red"]printf( "<td><a href='delete-reminder.php?remid=%d'>Delete</a></td>\n",$remid );[/COLOR]
        echo "</tr>\n";
    
    }
    mysql_free_result($result);
    
    ?>
    Code (markup):
     
    serjio28, Mar 28, 2007 IP