Jump to content

echo once inside a while loop.


seany123

Recommended Posts

Currently whether data from the queries are found or not, its surrounded inside the div tags...

 

i want to somehow echo the div tags inside the while loop, but only echo once.

 

the confusing part for me is that the queries are connecting to 2 different tables for data.. and both need to be found for the div to be echo.

 

if($item['quantity'] >= 1){

if($item2['type'] == 'weapon'){

 

 

code below....

 

 


<div class="g_content"><h3 style="text-align: left">  Weapons</h3><div class="g_text">
<table align='center' cellspacing='10'>
<?php

$current_col = 1;
$max_col = 4;


$query = $db->execute("select * from items where `player_id`=?", array($player->id));

while($item = $query->fetchrow())
{	
$query2 = $db->execute("select * from `blueprint_items` where `id`=?",array($item['item_id']));
$item2 = $query2->fetchrow();


//DISPLAY IF QUANTITY IS 1 OR MORE.
if($item['quantity'] >= 1){	
if($item2['type'] == 'weapon'){
    //Open new row if first column
    if($current_col==1)
    {
        echo "<tr>\n";
    }	

    //Display current record

    echo "<td width='25%'>";
echo "<center><img src=\"{$item2['img']}\" width='80' height='80' style=\"border: 1px solid #CC9900\"></center>";
    echo "<center><a href=\"../description.php?id={$item2['id']}\">{$item2['name']}</a> [x".$english_format_number = number_format($item['quantity'])."]</center>";
    echo "<center>$".$english_format_number = number_format($item2['value'])."</center>";
echo "<center>[<a href='../item.php?sell=".$item['id']."'>Sell</a>] [<a href='../item.php?market=".$item['id']."'>Market</a>] <br>[<a href='../item.php?send=".$item['id']."'>Send</a>] [<a href='../item.php?equip=".$item['id']."'>Equip</a>]</center><br>";
    echo "</td>\n";
 //Close row if last column
    if($current_col==$max_col)
    {
        echo "<tr>\n";
        $current_col = 0;  //<---Changed
    }
    $current_col++;
}
}

}
//Close last row if needed
if ($current_col!=1)
{
    for(; $current_col<=$max_col; $current_col++)
    {
        echo "<td> </td>\n";
    }
}

?>
</table>
</div></div>

Link to comment
Share on other sites

The way you are doing it is the right way. That is, wrap the entire loop in div tags if you want the content to be within a div. I can't see why you want to insert the div tags during the loop. That's useless complexity (KISS comes to mind).

 

What you want to do is do the while loop, and instead of echo'ing, store the html in a variable - like $html. Then, you can easily prefix and affix the div tags.

 

So echo would become:

 

$html = 'line1htmlcode';

$html .= 'somemorecode'; //note the '.' period

 

Then - add the tags:

 

$html = '<div>'.$html.'</div>';

 

Simples.

Link to comment
Share on other sites

Oh, but if you *must*, though I would advise against it.

 

You can use a counter like so:

 


$n = 0;
$while($item = $query->fetchrow()){

  echo ($n == 0) ? '<div>' : '';

  // do stuff here

  echo ($n == 0) ? '</div>' : '';

$n++;
}

 

Noted about your confusion. Regarding this, you need to carefully look at how your data is being obtained and structure your code properly. I can tell you that the current way isn't very optimal.

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.