Jump to content

Recommended Posts

Okay, this script is going to be pretty hard to explain, but I will try my best.

 

All users have there own inventory with items in it, with these items they may build things IF they have the required amount. I will post the code now and explain the rest after:

 

<?php

$buildingid=$_GET['buildingID'];
$query=mysql_query("select buildingname,resourcesneeded from buildings where buildingID='$buildingid'");
$row=mysql_fetch_array($query);

$name=explode("<br>", $row['resourcesneeded']);
$amountIn = count($name);
$numbers = array();
$items = array();
$yes=0;
$no=0;

echo "<table border=1 width='10%'>";
echo "<th>Item</th><th>Amount needed</th><th width=35>Have it?</th>";

for($x = 0; $x < $amountIn; $x++){

$strpos = strpos($name[$x]," ");
$number = substr($name[$x],'0',$strpos);
$item = substr($name[$x],$strpos);
$numbers[] = $number;
$items[] = $item;

print "$items[$x] - $numbers[$x]<p>";
echo "<tr><td class=tstyle3>$items[$x]</td><td class=tstyle3>$numbers[$x]</td>";

$query = mysql_query("SELECT COUNT(*) FROM items WHERE iname='$items[$x]' AND        ownerid='$sid'");

$num=mysql_fetch_array($query);

if($num[0] >= $numbers[$x]){
$yes++;
print "<td class=tstyle3>yes</td>";
}
else{
$no++;
print "<td class=tstyle3>no</td>";
}

print "</tr>";	

}
if($yes == $amountIn){
print "<tr><td class=tstyle3><form method=post action=carpenter.php?action=viewmyorders><input style='width=300' type=submit name=submit value='Build This For An Order!'></form></td></tr>";
}
else{
print "</table>You don't have the required items.";
}
echo "</table>";

?>

 

So basically this code will loop through and get what how much of each item the user needs to be able to build what they want to build and stores them in an array.

 

$amountin stores how much they need for that item

$items stores all the items names

$numbers stores all the amounts they need for the corresponding item in $items

 

My problem lies in this chunk of code that checks for the items:

 

<?php

$query = mysql_query("SELECT COUNT(*) FROM items WHERE iname='$items[$x]' AND        ownerid='$sid'");

$num=mysql_fetch_array($query);

if($num[0] >= $numbers[$x]){
$yes++;
print "<td class=tstyle3>yes</td>";
}
else{
$no++;
print "<td class=tstyle3>no</td>";
}

print "</tr>";	

}
if($yes == $amountIn){
print "<tr><td class=tstyle3><form method=post action=carpenter.php?action=viewmyorders><input style='width=300' type=submit name=submit value='Build This For An Order!'></form></td></tr>";
}
else{
print "</table>You don't have the required items.";
}
echo "</table>";

?>

 

As you can see, this query:

$query = mysql_query("SELECT COUNT(*) FROM items WHERE iname='$items[$x]' AND        ownerid='$sid'");

 

counts how many of the items they have in their inventory. If they have the right amount I want to display a submit button, and if they don't it says something like "you don't have the required amount of items".

 

The problem is it ALWAYS says they don't have the items when the person does. Anyone have any ideas on why this is happening?

 

I know this is a lot to look at, and I thank anyone who gives it a shot =D It is greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/39338-solved-array-problems/
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.