JPark Posted June 11, 2009 Share Posted June 11, 2009 I have a page that queries a mysql database. With the data that is pulled (image URLs and the web sites they're associated with), a table that is 2 columns wide. The number of rows in the table is calculated by counting the rows of data and dividing by 2. It works well -- with a small logic problem that I can't figure out... If the data is odd-numbered (7 items, 11 items, etc.), the last cell has a missing image (echo "<a href='".$url[$i]."'><img src='".$imageUrl[$i]."' border='0'></a>". If the data is even-numbered (8 items, 20 items, etc.), there is an extra -- and empty -- row. What am I missing?? $row = mysql_fetch_array($result) or die(mysql_error()); // mysql_fetch_array() returns both an assocative array and a numerically indexed array -- // a combination of the mysql_fetch_row() and the mysql_fetch_assoc() functions // set some basic variables; $tr=0; $flag= 0; $i=0; // run through the all the arrays to break them into individual items that I can reference later while($row = mysql_fetch_object( $result )) { $item[$flag]= $row->item; $title[$flag]= $row->title; $url[$flag]= $row->url; $imageUrl[$flag]= $row->imageUrl; $alt[$flag]= $row->alt; $sex[$flag]= $row->sex; $shirtType[$flag]= $row->shirtType; $flag++; } // find out how many rows we need $totalRows=($flag/2); // begin the table echo "<table border='2' cellpadding='5' align='center'>"; while ($tr <= $totalRows) { // create the rows $td=1; // each time through, re-start the column counter echo "<tr>"; while ($td <= 2) { //create the columns echo "<td>"; echo "<a href='".$url[$i]."'><img src='".$imageUrl[$i]."' border='0'></a>"; $i++; echo "</td>\n"; $td++; } echo "</tr>"; $tr++; } echo "</table>"; Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/161836-solved-dynamically-building-a-table/ Share on other sites More sharing options...
RichardRotterdam Posted June 11, 2009 Share Posted June 11, 2009 You find the following link usefull to solve your problem http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Quote Link to comment https://forums.phpfreaks.com/topic/161836-solved-dynamically-building-a-table/#findComment-853858 Share on other sites More sharing options...
JPark Posted June 12, 2009 Author Share Posted June 12, 2009 Thanks! But, I don't get this comment: "To modify this for an array, just replace the while loop with a for loop and use your array as the input instead of the mysql fetch results." What would I do on the script I attached earlier? What would I change? Quote Link to comment https://forums.phpfreaks.com/topic/161836-solved-dynamically-building-a-table/#findComment-854165 Share on other sites More sharing options...
Andy-H Posted June 12, 2009 Share Posted June 12, 2009 <?php /* START TABLE */ echo ' <table border="2" cellpadding="5" align="center"> <tr> '; $num = mysql_num_rows( $result ); $i = 0; while ( $row = mysql_fetch_object ( $result ) ) { $i++; if ( ($num % 2 != 0 ) && ( $i == $num ) ) { echo ' <tr> '; echo ' <td colspan="2"> '; echo ' <a href="' . $row->url . '" ><img src="' . $row->imageUrl . '" border="0" ></a> '; echo ' </td> '; echo ' </tr> '; } else { echo ' <td> '; echo ' <a href="' . $row->url . '" ><img src="' . $row->imageUrl . '" border="0" ></a> '; echo ' </td> '; if ( $i % 2 == 0 ) { echo ' </tr> '; if ( $i != $num ) { echo ' <tr> '; } } } } echo '</table>'; /* TABLE DONE */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/161836-solved-dynamically-building-a-table/#findComment-854186 Share on other sites More sharing options...
JPark Posted June 12, 2009 Author Share Posted June 12, 2009 thanks! Quote Link to comment https://forums.phpfreaks.com/topic/161836-solved-dynamically-building-a-table/#findComment-854418 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.