Jump to content

[SOLVED] help needed.


seany123

Recommended Posts

okay so basically im making this page show images from a database...

 

<div id="left_c"><div class="g_content"><h3>  Your Points</h3><div class="g_text"><table width='100%' align='center' cellspacing='10'><tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<?php
$query = $db->execute("select * from items where `player_id`=?", array($player->id));
while($item = $query->fetchrow())
{

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

echo "<td>";
echo "<a href='../item.php?id=".$item2['item_id']."'>".$item2['name']."</a><br>";
echo "<img src='" . $item2['img'] . "' width='100' height='100' style='border: 1px solid #cccccc'>";
}
?>
</tr>
</table>
</div></div></div>

 

but what i want is for when the code echos records, for it to echo it into a new row in my table above.

 

im not really sure i can get this working..

Link to comment
Share on other sites

I'm not sure what you mean when you say "what i want is for when the code echos records, for it to echo it into a new row in my table above."

 

Can I make a suggestion?  You might want to combine those queries with a LEFT JOIN to speed things up.

 

Link to comment
Share on other sites

$query=$db->execute(" SELECT b.* FROM `items` i
LEFT JOIN `blueprint_items` b ON (b.item_id=i.item_id)
WHERE (player_id=?", array($player->id));

 

this way you get item_id, name, and img right away without having to do a whole bunch of extra queries.

 

To limit your images to 4 per row you have a couple of options.  You can use a counter in your loop and insert a <br> every 4th image.  You can float left every image inside a div with a fixed width.  There may be more options but I can't think of them right now.

Link to comment
Share on other sites

Hmmm, the database class youare using makes it difficult to really understand what you are doing - especially the fact that you are passing an array to the function. Are those values for Player ID and Item ID really arrays or is that just how the functions work? I am guessing they are NOT arrays and have modified the code accordingly. (I also assumed the second parameter is not required in $db->execute().

 

<div id="left_c"><div class="g_content"><h3>  Your Points</h3><div class="g_text">
<table width="100%" align="center" cellspacing="10">
<?php

$current_col = 1;
$max_col = 4;

$query = "SELECT *
          FROM items
          LEFT JOIN `blueprint_items`
            ON items.item_id = blueprint_items.item_id
          WHERE `player_id` = '{$player->id}'";
$result = $db->execute($query);

while($record = $result->fetchrow())
{
    //Open new row if first column
    if($current_col==1)
    {
        echo "<tr>\n";
    }	

    //Display current record
    echo "<td>";
    echo "<a href=\"../item.php?id={$result['item_id']}\">{$result['name']}</a><br>";
    echo "<img src=\"{$result['img']}\" width=\"100\" height=\"100\" style=\"border: 1px solid #cccccc\">";
    echo "</td>\n";

    //Close row if last column
    if($current_col==$max_col)
    {
        echo "<tr>\n";
        $current_col = 1;
    }
    $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></div>

Link to comment
Share on other sites

Hmmm, the database class youare using makes it difficult to really understand what you are doing - especially the fact that you are passing an array to the function. Are those values for Player ID and Item ID really arrays or is that just how the functions work? I am guessing they are NOT arrays and have modified the code accordingly. (I also assumed the second parameter is not required in $db->execute().

 

<div id="left_c"><div class="g_content"><h3>  Your Points</h3><div class="g_text">
<table width="100%" align="center" cellspacing="10">
<?php

$current_col = 1;
$max_col = 4;

$query = "SELECT *
          FROM items
          LEFT JOIN `blueprint_items`
            ON items.item_id = blueprint_items.item_id
          WHERE `player_id` = '{$player->id}'";
$result = $db->execute($query);

while($record = $result->fetchrow())
{
    //Open new row if first column
    if($current_col==1)
    {
        echo "<tr>\n";
    }	

    //Display current record
    echo "<td>";
    echo "<a href=\"../item.php?id={$result['item_id']}\">{$result['name']}</a><br>";
    echo "<img src=\"{$result['img']}\" width=\"100\" height=\"100\" style=\"border: 1px solid #cccccc\">";
    echo "</td>\n";

    //Close row if last column
    if($current_col==$max_col)
    {
        echo "<tr>\n";
        $current_col = 1;
    }
    $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></div>

 

using that it now echo's nothing.

Link to comment
Share on other sites

I give no guarantee that my code will work, especially since I am trying to write it against your database. I merely have provided the framework for a solution. I leave it to you to properly implement. Did you do ANY debugging AT ALL? (Plus I did state that I had to make some assumptions because of the class you are using).

 

The first step would be to check the HTML created. If there is simply an opening and closing table tag, that would imply the query is not generating any results. If that's the case, then you should echo the query to the page to see that it is being created correctly and then to test it in the back end.

Link to comment
Share on other sites

I think he may have been implying more that you should not use a db class that you do not fully understand the workings of. People try and help, but when your stuff relies on classes and data they don't have access to, helping you troubleshoot is tough.

 

Aside from that, aggrav8d and mjdamato gave you the answer. You just need to create a counter to use with the output loop... when it hits the max number of images per row, start a new one. Sounds pretty straight forward :)

 

 

Link to comment
Share on other sites

In your code move the <tr> and </tr> within the while loop.

 

doing that will mean that it shows 1 database record per line. i want it to show 4.

 

seany123,

 

Did you even "read" the code provided above? As SaurabhAgarwal suggested it is the process that is important here. If you don't understand what the code is doing you shouldn't be using it. We tried to show a single query is more efficient for this purpose. I don't know anything about the database class you are using in order to properly implement that, but I have to believe it can work with a simple JOIN. But, you will have to figure that out yourself since you decided to use that class.

 

I could have simply rewrote your "looping queries" code above, but that would only reenforce bad programming habits. Heck, if you were to try and understand the code provided you should be able to do that yourself.

 

I did try and help you in my last post by asking you to check some things to see if we could narrow down the problem, but you failed to respond to any of those questions. I am more than happy to help, but for this problem to be solved will take participation on your part.

Link to comment
Share on other sites

In your code move the <tr> and </tr> within the while loop.

 

doing that will mean that it shows 1 database record per line. i want it to show 4.

 

seany123,

 

Did you even "read" the code provided above? As SaurabhAgarwal suggested it is the process that is important here. If you don't understand what the code is doing you shouldn't be using it. We tried to show a single query is more efficient for this purpose. I don't know anything about the database class you are using in order to properly implement that, but I have to believe it can work with a simple JOIN. But, you will have to figure that out yourself since you decided to use that class.

 

I could have simply rewrote your "looping queries" code above, but that would only reenforce bad programming habits. Heck, if you were to try and understand the code provided you should be able to do that yourself.

 

I did try and help you in my last post by asking you to check some things to see if we could narrow down the problem, but you failed to respond to any of those questions. I am more than happy to help, but for this problem to be solved will take participation on your part.

 

after reading all the posts again i did look over the code you gave me again and decided that your solution is the only one available to me at the moment...

 

so ive done exactly what you wrote except DIDNT use the JOIN query.. i instead just kept my queries and changed the values around.... anyway....

 

 

its basically done what i want except 1 little error, for some reason it started a new line and only showed 3 values instead of making it show 4....

 

im probably confusing you alot right now so ive taken a picture to show what i mean.

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

I see the problem. Within the small block that closes a row (i.e. </TR>) the code is changing the value of $current_col to 1, but then it is immediately increased by 1 after that block (2).

 

Change this

    //Close row if last column
    if($current_col==$max_col)
    {
        echo "<tr>\n";
        $current_col = 1;
    }
    $current_col++;

To this:

    //Close row if last column
    if($current_col==$max_col)
    {
        echo "<tr>\n";
        $current_col = 0;  //<---Changed
    }
    $current_col++;

The value will be set to 0, but then immediately increased to 1 (which is correct since the next item will be the first column.

Link to comment
Share on other sites

Yes that worked a treat thanks...

 

just one more thing..

 

is there a way to work it so, if a query finds say $item['id'] more than once it will only echo the db details one time but echo something like the amount of records it found.

Link to comment
Share on other sites

You can do that in PHP, but that would be stupid when you are using a database which does this naturally.

 

You know it would have been helpful had you responded appropriately to my previous comment

Are those values for Player ID and Item ID really arrays or is that just how the functions work?

 

Since you did not provide the information up front I went the extra step to at least tell you what my assumptions were. But, then you failed to correct my false assumption. Had you done so we would have solved this earlier. Now that I know those values really are arrays, I'm pretty sure this will work to 1) use a single query and 2) add the ability to group the records.

 

<div id="left_c"><div class="g_content"><h3>  Your Points</h3><div class="g_text">
<table width="100%" align="center" cellspacing="10">
<?php

$current_col = 1;
$max_col = 4;

$query = "SELECT b.item_id, b.name, b.img, COUNT(b.item_id) as item_count
          FROM items i
          LEFT JOIN `blueprint_items` b
            ON items.item_id = blueprint_items.item_id
          WHERE i.player_id IN ('" . implode("', '", array($player->id)) . "')";

$result = $db->execute($query);

while($record = $result->fetchrow())
{
    //Open new row if first column
    if($current_col==1)
    {
        echo "<tr>\n";
    }	

    //Display current record
    echo "<td>";
    echo "<a href=\"../item.php?id={$result['item_id']}\">{$result['name']} ({$result['item_count']})</a><br>";
    echo "<img src=\"{$result['img']}\" width=\"100\" height=\"100\" style=\"border: 1px solid #cccccc\">";
    echo "</td>\n";

    //Close row if last column
    if($current_col==$max_col)
    {
        echo "<tr>\n";
        $current_col = 1;
    }
    $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></div>

 

I give no guarantees this will work. but if it doesn't at least provide soe information of what didn't work right and do some simple debugging in order to help you further. For example, echo out the query so we can ensure it is being created properly

Link to comment
Share on other sites

You can do that in PHP, but that would be stupid when you are using a database which does this naturally.

 

You know it would have been helpful had you responded appropriately to my previous comment

Are those values for Player ID and Item ID really arrays or is that just how the functions work?

 

Since you did not provide the information up front I went the extra step to at least tell you what my assumptions were. But, then you failed to correct my false assumption. Had you done so we would have solved this earlier. Now that I know those values really are arrays, I'm pretty sure this will work to 1) use a single query and 2) add the ability to group the records.

 

<div id="left_c"><div class="g_content"><h3>  Your Points</h3><div class="g_text">
<table width="100%" align="center" cellspacing="10">
<?php

$current_col = 1;
$max_col = 4;

$query = "SELECT b.item_id, b.name, b.img, COUNT(b.item_id) as item_count
          FROM items i
          LEFT JOIN `blueprint_items` b
            ON items.item_id = blueprint_items.item_id
          WHERE i.player_id IN ('" . implode("', '", array($player->id)) . "')";

$result = $db->execute($query);

while($record = $result->fetchrow())
{
    //Open new row if first column
    if($current_col==1)
    {
        echo "<tr>\n";
    }	

    //Display current record
    echo "<td>";
    echo "<a href=\"../item.php?id={$result['item_id']}\">{$result['name']} ({$result['item_count']})</a><br>";
    echo "<img src=\"{$result['img']}\" width=\"100\" height=\"100\" style=\"border: 1px solid #cccccc\">";
    echo "</td>\n";

    //Close row if last column
    if($current_col==$max_col)
    {
        echo "<tr>\n";
        $current_col = 1;
    }
    $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></div>

 

I give no guarantees this will work. but if it doesn't at least provide soe information of what didn't work right and do some simple debugging in order to help you further. For example, echo out the query so we can ensure it is being created properly

 

to be honest, im just not that confident using that LEFT JOIN query thing.... ive never wrote queries in that way and i dont understand how they work.

 

im sorry if i was being shit at explaining what i was doing... but i would like to compliment you for your persistence and general help.

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.