1

Can someone help me on this. I'm made an image uploader and i want the image to make another tr if it reach to 5 pics so it will not overflow. Here is my code:

$dbc = mysql_connect("localhost" , "root" , "") or die (mysql_error());
mysql_select_db('blog_data') or die (mysql_error());

$sql = "SELECT * FROM img_uploaded";
$result = mysql_query($sql);

while($rows=mysql_fetch_array($result))
{
  if ($rows)
  {
    echo "<tr><td><img src='user_images/".$rows['img_name'] . "' width='100' height='100'></td></tr>";
  }
  else
  {
    echo "<td><img src='user_images/".$rows['img_name'] . "' width='100' height='100'></td>";
  }
}

mysql_close();

3 Answers 3

1

E.g. by using the modulus operator:

$dbc = mysql_connect("localhost" , "root" , "") or die (mysql_error());
mysql_select_db('blog_data', $dbc) or die (mysql_error($dbc));

$sql = "SELECT * FROM img_uploaded";
$result = mysql_query($sql, $dbc) or die(mysql_error($dbc));

echo '<table><tr><th>image</th>';
for($cnt=0; false!==($row=mysql_fetch_array($result)); $cnt++) {
  if ( 0===$cnt%5 ) {
    echo '</tr><tr>';
  }
  echo '<td><img src="user_images/'.$rows['img_name'] . '" width="100" height="100"></td>';
}
echo '</tr></table>';
Sign up to request clarification or add additional context in comments.

Comments

1

It uses the modulus operator, but in addition it checks that a has been opened.

$dbc = mysql_connect("localhost" , "root" , "") or die (mysql_error());
mysql_select_db('blog_data') or die (mysql_error());

$sql = "SELECT * FROM img_uploaded";
$result = mysql_query($sql);

$numOfRows = 0;

while($rows = mysql_fetch_array($result))
{
    if (($numOfRows % 5) === 0)
    {
        if ($numOfRows != 0)
        {
            echo '</tr>';
        }

        echo '<tr>';
    }

    $numOfRows++;

    if ($rows)
    {
        echo "<td><img src='user_images/".$rows['img_name'] . "' width='100' height='100'></td>";
    }
    else
    {
        echo "<img src='user_images/".$rows['img_name'] . "' width='100' height='100'>";
    }
}

mysql_close();

Comments

1
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
    $rows[] = $row;
}
$cols = 5;
$chunkSize = ceil(count($areaArray) / $cols);
echo $chunkSize * $cols;
foreach (array_chunk($rows, $chunkSize) as $itemsInThisTr) : ?>
 <tr>
<?php foreach ($itemsInThisTr as $item) : ?>
   <td><?php echo $item['img_name']; ?></td>
<?php endforeach; ?>
 </tr>
<?php endforeach; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.