Jump to content

Help needed with html to php echo code and images in a row display the same img


The_Dude_1978

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.