Question

I'm trying to figure out how I would do a foreach statement inside my while statement. As you can tell by this code, it will only submit 1 row out of the table, even though it selects all the rows. How would I make it select each row?

Code:

$q2 = mysql_fetch_array(mysql_query("SELECT * FROM `ticketreply` WHERE `ticketid`='$id'"));
$num_rows = mysql_num_rows($q2);
while(count($num_rows) > $i){
    echo "<div style='float:left;width:940px;margin-bottom:2%;margin-left:".($i + 1)."0px;margin-right:25%;'><div style='margin-left:".($i + 1)."0px;border:1px solid #cecece;padding:10px;'>Posted By: <a href='#'>".user2($q2[1])."</a><h2>".$q2[2]."</h2></div></div></div>";
    $i++;
}

New Attempt:

$q2 = mysql_query("SELECT * FROM `ticketreply` WHERE `ticketid`='$id'");
$i = 0;
foreach($q2 as $s){
    echo "<div style='float:left;width:100%;margin-bottom:2%;margin-left:".($i + 1)."0px;margin-right:25%;'><div style='margin-left:".($i + 1)."0px;border:1px solid #cecece;padding:10px;'>Posted By: <a href='#'>".user2($s[1])."</a><h2>".$s[2]."</h2></div></div></div>";
    $i++;
}

Although now this doesn't display any rows.

Was it helpful?

Solution

$q2 = mysql_query("SELECT * FROM `ticketreply` WHERE `ticketid`='$id'");

$i = 0;
while ($s = mysql_fetch_array($q2, MYSQL_NUM)) {
    echo "<div style='float:left;width:100%;margin-bottom:2%;margin-left:".($i + 1)."0px;margin-right:25%;'><div style='margin-left:".($i + 1)."0px;border:1px solid #cecece;padding:10px;'>Posted By: <a href='#'>".user2($s[1])."</a><h2>".$s[2]."</h2></div>   </div></div>";
    $i++
}

OTHER TIPS

Just to be clear, your query, SELECT * FROMticketreplyWHEREticketid='$id' is most likely only selecting one row from the table. I'm assuming that the ticketid in your ticketreply table is unique. So there is only one row selected.

If I go with this assumption, then...

mysql_fetch_array fetches all of the columns from a single row returned by a query. Documentation on mysql_fetch_array can be found here.

I would recommend, before you dive right into outputting your HTML and styling, start first with some print statements of $q2, to see that it really contains what you expect it to contain, and has the structure that you expect it to have.

Try:

$q2 = mysql_fetch_array(mysql_query("SELECT * FROM `ticketreply` WHERE `ticketid`='$id'"));
print_r($q2);

You should see that $q2 is an array that is both integer-indexed and also string-indexed (that is, it is also a hash where each column name in your table is a key). Let's restrict mysql_fetch_array so that it just gives us the associated array (the string-indexed part) and not the integers.

$q2 = mysql_fetch_array(mysql_query("SELECT * FROM `ticketreply` WHERE `ticketid`='$id'"), MYSQL_ASSOC);

Now, you can access each element of $q2 as a key-value pair by the following:

foreach ($q2 as $column => $value) {
  print "column name: $column\n";
  print "      value: $value\n";
  print "\n";
}

If, instead, you don't care about the column names, and you just want an integer-indexed array of values, then you can do the following:

$q2 = mysql_fetch_array(mysql_query("SELECT * FROM `ticketreply` WHERE `ticketid`='$id'"), MYSQL_NUM);

Then, you can use a regular for loop for iterating through your result...

for ($i = 0; $i < sizeof($q2); $i++) {
   print "Value $i: " . $q2[$i] . "\n";
}

Once you get a good feel for what the structure of $q2 is, then you will probably feel pretty comfortable and confident outputting your HTML with values from $q2 embedded in the right place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top