Re: another PHP Sql question...

From: Date: Fri, 29 Aug 2003 14:17:53 +0000
Subject: Re: another PHP Sql question...
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-30232@lists.php.net to get a copy of this message
On Friday, Aug 29, 2003, at 02:14 US/Eastern, jsWalter wrote:
But now I've been thrown a wrench in the form of double row display. my client wants...
    a   b
    c   d
    e   f
... .. I need to create... <tr>
      <td>row_1/feild_A</td>
      <td>row_1/feild_B</td>
</tr> <tr>
      <td>row_2/feild_A</td>
      <td>row_2/feild_B</td>
</tr> ... And using the method of WHILE() doesn't cut it. Can some one enlighten me what syntax can I use to hit 2 rows at a time?
Use "for". You'll need to know the number of rows returned by your query. Using MySQL, I find that out using mysql_num_rows($query). You'll need to adapt this for your database. So I would do it this way (probably not the most elegant, but it works): $your_query = mysql_query("select your data from your table",$db); // Run the query $row_count = mysql_num_rows($your_query); // Count rows returned $table_row_index = 1; // We'll use this to count your table rows echo "<tr>\r"; for ($i=0; $i<$row_count; $i++) { // Start with $i = 0, do while $i < number of rows, and increment $i with each loop $your_row = mysql_fetch_array($your_query); $field = $your_row['field']; echo "<td>Row_$table_row_index/$field</td>\r"; // This is all stuff you've done // so far we've just returned values from your query and written them to a table cell // Here's the trick: if $i is even, we're on the first cell of a row. If $i is odd, // we're on the second cell. We determine this with fmod($i/2); if the remainder // (the result) is 0, $i is even, if it's 1, $i is odd. if (fmod($i/2) != 1)) { // if $i is odd, we need to end this row and start another. echo "</tr>\r<tr>\r"; $table_row_index++; // Increment your row numbering. } } echo "</tr>\r"; Sorry about the weird comment wraps. I hope this makes sense. pjm

« previous php.db (#30232) next »