The_Dude_1978 Posted August 25, 2011 Share Posted August 25, 2011 Hi There, I'm trying to do 2 things at the same time here. This piece of code i'm having trouble with to convert it to php echo statement. '<div class="price">€'.number_format($row['price'], 2).'</div><a href=addToCart.php?item='.$file['item'].'">Add to cart</a>'; And with this code i'm getting 3x3 images, but per row the images are all the same, which is not my intention. I can't seem to find out what i'm doing wrong. Can you please help me? function getProducts() { //Function to display the products on the front end //Create the MYSQL db connection $db = new Connection(DB_HOST, DB_USER, DB_PASS, T4_DB_NAME); //Loop through the mysql results if($_GET['id']) { $sql="SELECT `id`, `first`, `last`, `username`, `email`, `about`, `level` from `users` WHERE `id` = '" . mysql_real_escape_string( $_GET [ "id" ]) . "'"; $res=mysql_query($sql); $row=mysql_fetch_assoc($res); { $res2 = mysql_query("SELECT `item`, `profile_id`, `title`, `size`, `type`, `thumbnail`, `reference`,`price` FROM user_photos WHERE profile_id = '" . mysql_real_escape_string( $_GET [ "id" ]) . "'"); $rows = mysql_num_rows($res2); if(mysql_num_rows($res2) > 0) { while($file = mysql_fetch_array($res2)) { $counter = 1; $cols = 3; echo "<table>\n"; for($i = 0; $i < $rows/$cols; $i++) { echo "<tr>"; for($j=0; $j < $cols && $counter <= $rows ;$j++, $counter++) { echo "<td>"; echo "<div class=\"product\">"; echo '<h3>'; echo "".$file['title'].""; echo '</h3>'; echo "<div class=\"info\">"; echo "<a href=\"/secure/users/".$row['username']."/pics/".$file['reference']."\"/><img src=\"/secure/users/".$row['username']."/pics/thumbs/".$file['reference']."\"/></a>"; '<div class="price">€'.number_format($row['price'], 2).'</div><a href=addToCart.php?item='.$file['item'].'">Add to cart</a>'; echo '</div>'; echo '</div>'; echo "</td>\n"; } echo "</tr>\n"; } echo "</table>\n"; } } } } } Kind regards, Martijn Link to comment https://forums.phpfreaks.com/topic/245710-help-needed-with-html-to-php-echo-code-and-images-in-a-row-display-the-same-img/ Share on other sites More sharing options...
xyph Posted August 25, 2011 Share Posted August 25, 2011 Here's how I deal with tables using an array as a source. It shouldn't be hard to move this over to a MySQL result loop <?php // Make dummy array $array = range(0,32); // This will keep track of how many rows we've echo'd $i = 0; // This tells us how many columns $cols = 3; $total = count( $array ); echo '<table><tr>'; foreach( $array as $value ) { // Echo the value in a cell echo '<td style="border:1px solid black;">'.$value.'</td>'; // Increase our counter by 1 before we check $i++; // Check to see if we need to start a new row by using the modulus operator // It'll give us the remainder of the two numbers divided. // We check if $i != $total to make sure we don't have a redundant empty row // at the end if( ($i % $cols) == 0 && $i != $total ) echo '</tr><tr>'; // Start a new row } // Now after the foreach, if we have an odd number of results, we'll have an // extra empty column we need to fill, we can do this automatically while( ($i % $cols) != 0 ) { echo '<td> </td>'; $i++; } echo '</tr></table>'; ?> Link to comment https://forums.phpfreaks.com/topic/245710-help-needed-with-html-to-php-echo-code-and-images-in-a-row-display-the-same-img/#findComment-1262034 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.