Jump to content

Recommended Posts

I know it sounds stupid, but let me explain.

For a project myself and obsidian are working on, I have come across a problem. I wont explain the whole project here but here is the problem:

I need to display 6 "stables", in as in three rows of two. I have done this with HTML, however each user will at some point buy these "stables" so they need to be dynamic, as some of them have a different background image as they are unavailable.

They may only have bought 3 stables, so the first two of the stables on the first row are available, and the first on the second row. This is a little hard to explain in words, so:

Available | Available
Available | Unavailable
Unavailable | Unavailable

This is all OK, I have managed to generate all the stables with some available and some unavailable depending on how many the user has bought. But here comes the problem.... The user will have an "animal" that will be in one of the stables. However, they may not have an animal in all of their available stables.

Both the stables and animals are stored in a database, so I run the query to get the stables and in a while() loop echo the actual stables in HTML. But I need to then run the query for the "animals" to be in the stables at the same time. I dont think it is possible to run two conditions in a while() loop, and the way the HTML is constructed I cannot put the loop inside the other.

PLEASE HELP! I can provide a link and code if needed.
Sure, below is the PHP code. I apologise if it is a little confusing...

[code]<?php
function showStables($user_id) {

// Check if user has any stables to start with
$sql = mysql_query("SELECT * FROM choco_stables WHERE stable_user = ".$user_id." ORDER BY stable_id") or die(mysql_error());

// Number of stables...
$numStables = mysql_num_rows($sql);

// If no stables...
if($numStables == 0) {

print("
<div style='width:515px; padding:5px; background-color:#EFEFEF; border:1px solid #999999;'>
You do not have any stables. In order to look after your Chocobos, you will need to purchase one. However, as it is your first one, we will give you one for <b>free</b>, but in the future, if you want more, it will cost you <b>2,500 gil</b> for each one!<br />
<center>
<input type='button' class='submits' style='width:200px; margin-top:15px; margin-bottom:15px; font-size:14px; background-image:url(images/orange_fade.jpg); font-weight:bold' value='Get My First Stable' />
</center>
</div>
");

// Already have some stables...
} else {

// Start variables...
$counter = 1;
$per_row = 2;
$stableCount = 1;

// Start table...
$temp = '<table align="center" width="400" border="0" cellspacing="0" cellpadding="0" style="background-image:url(images/stable_floor.gif)">';

while($row = mysql_fetch_assoc($sql)) {

$stableLeft = rand(1, 3);
$stableRight = rand(4, 6);

if($counter == 1) {
$temp .= "<tr>";
}

if(($stableCount % 2) == 1) {
$temp .= '<td width="200" valign="top" class="stable" style="background-image:url(images/stable_background_'.$stableLeft.'.gif);">';
// Here is where each animal will be displayed on the left...
$temp.= '</td>';
} else {
$temp .= '<td width="200" valign="top" class="stable" style="background-image:url(images/stable_background_'.$stableRight.'.gif);">';
// Here is where each animal will be displayed on the right...
$temp.= '</td>';
}

if($counter == $per_row) {
$temp .= "</tr>";
$counter = 1;
} else { 
$counter++; 
}

// Increase counter
$stableCount++;

}

if($counter != 1) { 

while($counter <= $per_row) { 

// Odd number of stables?
if(($numStables % 2) == 1) {
$temp .= '
<td valign="top" class="stable" style="background-image:url(images/stable_background_dull_right.gif);">
&nbsp;
</td>
';
$counter++;
}
$temp .= "</tr>";

}

}

$temp .= "</table>";

print($temp);

}

}

?>[/code]
Sorry, I misunderstood. That may help above, though.

Yes, I have one table which stores the users, simply [b]users[/b], which contains the usual, [i]user_id, user_name, user_email[/i].

For the stables, the table is [b]choco_stables[/b]:
[i]stable_id (Primary), stable_user (Foreign)[/i]

Obviously the "stable_user" relates to the "user_id".

And finally the actual animals, is the table [b]choco_chocobos[/b]:
[i]choco_id (Primary), choco_owner (Foreign), choco_level, choco_name...[i]

Hopefully that may be a little more helpful. :)
If I understand your tables correctly, here's the basic loop I would use (you would have to fit your display requirements in):
[code]<?php
$q1 = "Select * from choco_stables";
$rs1 = mysql_query($q1) or die("Problem getting stables, query: $q1<br>" . mysql_error());
while ($rw1 = mysql_fetch_assoc($rs1)) {
    if ($rw1['stable_user'] > 0) { // or however you test for an owned stable
        $q2 = "select * from users where user_id = " . $rw1['stable_user'];
        $rs2 = mysql_query($q2) or die("Problem getting owner of stable, query: $q2<br>" . mysql_error());
        $rw2 = mysql_fetch_assoc($rs2);
        $q3 = "select * from choco_chocobos where choco_owner = " . $rw2['user_id'];
        $rs3 = mysql_query($q3) or die("Problem getting animal, query: $q3<br>" . mysql_error());
        if (mysql_num_rows(rs3) > 0) { // This person owns an animal
            $rw3 = mysql_fetch_assoc($rs3);
//
//          more code associated with the animal
//
          }
//
//          more code associated with the owner of the stable
//
    }
?>[/code]

Ken
Thanks, Ken.

When I added the [b]echo $rw3['choco_name'];[/b] part, it echoes the name of the animal three times. This is not right, it has the number of stables right, but it should only echo the name once.

Any other ideas?

[code]<?php
$q1 = "Select * from choco_stables";
$rs1 = mysql_query($q1) or die("Problem getting stables, query: $q1<br>" . mysql_error());
while ($rw1 = mysql_fetch_assoc($rs1)) {
    if ($rw1['stable_user'] > 0) { // or however you test for an owned stable
        $q2 = "select * from users where user_id = " . $rw1['stable_user'];
        $rs2 = mysql_query($q2) or die("Problem getting owner of stable, query: $q2<br>" . mysql_error());
        $rw2 = mysql_fetch_assoc($rs2);
        $q3 = "select * from choco_chocobos where choco_owner = " . $rw2['user_id'];
        $rs3 = mysql_query($q3) or die("Problem getting animal, query: $q3<br>" . mysql_error());
        if (mysql_num_rows($rs3) > 0) { // This person owns an animal
            $rw3 = mysql_fetch_assoc($rs3);
echo $rw3['choco_name'];
//
//          more code associated with the animal
//
          }
//
//          more code associated with the owner of the stable
//
}

    }
}
?>[/code]
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.