Jump to content

Recommended Posts

Hello guys.. I need some idea on how will I able to do this

 

I have this code:

 

<?php
<table width="700">

<tr>
<td width="140"></td>
<td width="140"></td>
<td width="140"></td>
<td width="140"></td>
<td width="140"></td>
</tr>


while(list($item_id,$item_code,$item_price,$item_stock,$item_sale,$item_reg,) = mysql_fetch_row($result))
{


echo "<td align=\"center\">";

    echo "<img src=".$png." id=\"resizeMe\" />";
	echo "<br><span class=\"admin_list\"><u>". $item_code ."</span></u>";
	echo "<br>P <b>". (int)$item_price ."</b>";
	echo "<br>Stocks: <b>". $item_stock ."</b>";
	echo "<br>Sale? ";
	if($item_sale == 0){ echo "<b>NO</b>"; }else{ echo "<b>YES</b>"; }

	echo "<br><span class=\"admin_list_date\">Date Added: <b>". $time_reg ."</span></b>";

	echo "<br><a href=\"edit_item.php?id=". urlencode($item_id) ."\">[edit]</a> ";
	echo "<a href=\"delete_item.php?id=". urlencode($item_id) . "\"onclick=\"return confirm('Are you sure you delete item ". $item_id ." ?');\">    [delete]</a> ";
	echo "<a target=\"_blank\" href=\"edit_item.php?id=". urlencode($item_id) ."\">[view]</a>";



echo "</td>";



}





</table>
?>

 

 

After counting 5 rows I want to move to the next row of the table like this:

 

 

<tr>item1 item2 item3 item4 item5</tr>

<tr>item6 item7 item8 item9 item10</tr>

<tr>item11 item12                          </tr>

 

 

:confused:

 

 

Link to comment
https://forums.phpfreaks.com/topic/266307-move-to-next-line-after-counting-5-rows/
Share on other sites

Give this a try

$max_columns = 5;
$col_width = 140;
$table_width = $col_width * $max_columns;

//Open table
echo "<table width='{$table_width}'>\n";
//Create header row
echo "<th>\n";
for($col=0; $col<$max_columns; $col++)
{
    echo "<td width='{$col_width}'></td>\n";
}
echo "<th>\n";

//Process query results
$recCnt = 0;
while($row = mysql_fetch_assoc($result))
{
    $recCnt++;
    //Open new row if needed
    if($max_columns%$recCnt == 1)
    {
        echo "<tr>\n";
    }
    
    $price = (int) $row['item_price'];
    $sale = ($row['item_sale']) ? 'YES' : 'NO';
    $item_id_url = urlencode($row['item_id']);
    echo "<td align='center'>\n";
    echo "<img src='{$png}' id='resizeMe' /><br>\n";
    echo "<span class='admin_list'><u>{$row['item_code']}</u></span><br>\n";
    echo "P <b>{$price}</b><br>\n";
    echo "Stocks: <b>{$row['item_stock']}</b><br>\n";
    echo "Sale? <b>{$sale}</b><br>\n";
    echo "<span class='admin_list_date'>Date Added: <b>{$row['time_reg']}</b></span><br>\n";
    echo "<a href='edit_item.php?id={$item_id_url}'>[edit]</a> \n";
    echo "<a href='delete_item.php?id={$item_id_url}' onclick='return confirm('Are you sure you delete item {$row['item_id']}?');'>    [delete]</a> \n";
    echo "<a target='_blank' href='edit_item.php?id={$item_id_url}'>[view]</a>\n";
    echo "</td>\n";
    
    //Close row if needed
    if($max_columns%$recCnt == 0)
    {
        echo "</tr>\n";
    }
}

//Close last row if needed
if($max_columns%$recCnt == 0)
{
    echo "</tr>\n";
}

//Close table
echo "</table>\n"

Or try my:

 

<?php
echo '<table>';
echo '<tr>';
while(list($item_id,$item_code,$item_price,$item_stock,$item_sale,$item_reg,) = mysql_fetch_row($result)) {
echo "<td align=\"center\">";
echo "<img src=".$png." id=\"resizeMe\" />";
echo "<br><span class=\"admin_list\"><u>". $item_code ."</span></u>";
echo "<br>P <b>". (int)$item_price ."</b>";
echo "<br>Stocks: <b>". $item_stock ."</b>";
echo "<br>Sale? ";
if($item_sale == 0){ echo "<b>NO</b>"; }else{ echo "<b>YES</b>"; }
echo "<br><span class=\"admin_list_date\">Date Added: <b>". $time_reg ."</span></b>";
echo "<br><a href=\"edit_item.php?id=". urlencode($item_id) ."\">[edit]</a> ";

echo "<a href=\"delete_item.php?id=". urlencode($item_id) . "\"onclick=\"return confirm('Are you sure you delete item ". $item_id ." ?');\">    [delete]</a> ";

echo "<a target=\"_blank\" href=\"edit_item.php?id=". urlencode($item_id) ."\">[view]</a>";

echo "</td>";
if($item_id%5 == 0) {echo '</tr><tr>';}
}
echo '</tr>';
echo '</table>';
?>



Give this a try

$max_columns = 5;
$col_width = 140;
$table_width = $col_width * $max_columns;

//Open table
echo "<table width='{$table_width}'>\n";
//Create header row
echo "<th>\n";
for($col=0; $col<$max_columns; $col++)
{
    echo "<td width='{$col_width}'></td>\n";
}
echo "<th>\n";

//Process query results
$recCnt = 0;
while($row = mysql_fetch_assoc($result))
{
    $recCnt++;
    //Open new row if needed
    if($max_columns%$recCnt == 1)
    {
        echo "<tr>\n";
    }
    
    $price = (int) $row['item_price'];
    $sale = ($row['item_sale']) ? 'YES' : 'NO';
    $item_id_url = urlencode($row['item_id']);
    echo "<td align='center'>\n";
    echo "<img src='{$png}' id='resizeMe' /><br>\n";
    echo "<span class='admin_list'><u>{$row['item_code']}</u></span><br>\n";
    echo "P <b>{$price}</b><br>\n";
    echo "Stocks: <b>{$row['item_stock']}</b><br>\n";
    echo "Sale? <b>{$sale}</b><br>\n";
    echo "<span class='admin_list_date'>Date Added: <b>{$row['time_reg']}</b></span><br>\n";
    echo "<a href='edit_item.php?id={$item_id_url}'>[edit]</a> \n";
    echo "<a href='delete_item.php?id={$item_id_url}' onclick='return confirm('Are you sure you delete item {$row['item_id']}?');'>    [delete]</a> \n";
    echo "<a target='_blank' href='edit_item.php?id={$item_id_url}'>[view]</a>\n";
    echo "</td>\n";
    
    //Close row if needed
    if($max_columns%$recCnt == 0)
    {
        echo "</tr>\n";
    }
}

//Close last row if needed
if($max_columns%$recCnt == 0)
{
    echo "</tr>\n";
}

//Close table
echo "</table>\n"

 

 

Psycho thanks.

 

On that code, will it work if I use

while(list($item_id,$item_code,$item_price,$item_stock,$item_sale,$item_reg,) = mysql_fetch_row($result))

 

instead of

 

while($row = mysql_fetch_assoc($result))

 

 

I will try this later once I got home.

Your code is unreadable, illogically structured, deprecated, and incorrect.

 

WHOA!!!!!! pissing contest?

 

personally, I would only say something like that, if I could PROVE:

 

if unreadable, well, nothing else you post is relevant.  how is it incorrect, if no read?

 

illogically st...  ok, spock, define structure(d).  we can go from there.

 

deprecated?  nobody who knows that word would use it as you have.  plus, even if deprecated, still works.

Psycho thanks.

 

On that code, will it work if I use

while(list($item_id,$item_code,$item_price,$item_stock,$item_sale,$item_reg,) = mysql_fetch_row($result))

 

instead of

 

while($row = mysql_fetch_assoc($result))

 

Yes, but that structure is poor form. Using that type of logic could cause problems later on.

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.