PDA

View Full Version : MySQL/PHP Variable Question


Graphics
02-01-2003, 01:32 PM
OK, I'm making my own Itemshop. But I'm not the best coder around and need some help with something simple.

I need multiple things to show up from the same MySQL table column. For example, here's a part of my file:


// select information
$shop_id = $DB_site->query("SELECT shop_id FROM itemshops");
$shop_name = $DB_site->query("SELECT shop_name FROM itemshops");
$shop_image = $DB_site->query("SELECT shop_image FROM itemshops");
$shop_rating = $DB_site->query("SELECT shop_rating FROM itemshops");
$shop_profit = $DB_site->query("SELECT shop_profit FROM itemshops");
$shop_total = $DB_site->query("SELECT shop_total FROM itemshops");


Now, if I have, for example, 3 Itemshops, how would I get the three Itemshop's attributes to show? Use the variables three times? :ermm:

Thanks.

mr e
02-01-2003, 03:02 PM
here try this


$shops = $DB_site->query("SELECT * FROM itemshops DESC LIMIT 3");
while ($shop = $DB_site->fetch_array($shops)) {
$i = 1;
$shop_id$i = $shop[shop_id];
$shop_name$i = $shop[shop_name];
$shop_image$i = $shop[shop_image];
$shop_rating$i = $shop[shop_rating];
$shop_profit$i = $shop[shop_profit];
$shop_total$i = $shop[shop_total];
$i++;
}


that will select the first 3 rows from your database and put each variable into for example

$shop_id1 will be the first shops id
and
$shop_total2 will be the second shops total

im pretty sure this should work

Graphics
02-01-2003, 03:05 PM
So then for the other Itemshops I'd just simply repeat that except change the value of $i to 2?

Or am I getting this wrong? :p

EDIT: Or change DESC LIMIT to 4 or above to include extra Itemshops?

Graphics
02-01-2003, 03:37 PM
OK, this is me testing your method.


<?php

require("./global.php");

$shops = $DB_site->query("SELECT * FROM itemshops DESC LIMIT 3");
while ($shop = $DB_site->fetch_array($shops)) {
$i = 1;
$shop_id$i = $shop[shop_id];
$shop_name$i = $shop[shop_name];
$shop_image$i = $shop[shop_image];
$shop_rating$i = $shop[shop_rating];
$shop_profit$i = $shop[shop_profit];
$shop_total$i = $shop[shop_total];
$i++;
}
?>
<html>
<head>
</head>
<body>
$shop_name2</body></html>


But I get an error:


Parse error: parse error, unexpected T_VARIABLE in /home/virtual/site139/fst/home/staticnetwork/public_html/forum/test.php on line 8

:(:( Any ideas? Gurus? ;)

mr e
02-01-2003, 04:22 PM
do this for all of them


$shop_id[$i]


i think that should work

Graphics
02-02-2003, 12:14 PM
Hmmm... Well Here's my full file:


<?php

require("./global.php");

$shops = $DB_site->query("SELECT * FROM itemshops");
while ($shop = $DB_site->fetch_array($shops)) {
$i = 1;
$shop_id[$i] = $shop[shop_id];
$shop_name[$i] = $shop[shop_name];
$shop_image[$i] = $shop[shop_image];
$shop_rating[$i] = $shop[shop_rating];
$shop_profit[$i] = $shop[shop_profit];
$shop_total[$i] = $shop[shop_total];
$i++;
}
?>
<html>
<head>
</head>
<body>
<?php
echo "$shop_id[$i]";
?>
</body></html>

But it just doesn't show anything at all when I load up the page... :(

Graphics
02-02-2003, 01:39 PM
Anyone? This is really bugging me and I need answers fast. ;) Gotta finish this by the date... :(

mr e
02-02-2003, 05:58 PM
you can't echo $shop_id[$i], it has to be $shop_id[0] or [1] etc, but are you going to use these variables in repeating rows or at all different parts of the page? like are you going to use $shop_id[0] at one part and $shop_id[1] in some completely different part?

Graphics
02-03-2003, 03:08 PM
It's be like:


<tr>
<td>
$shop_id[0]
</td>
</tr>
<tr>
<td>
$shop_id[1]
</td>
</tr>


So they're not that far away. What's the diffrence anyway?

mr e
02-03-2003, 09:16 PM
because if your going to do this in repeating rows there is a much easier way, this way lets you use these variables anywhere you want, if you want to do repeating rows, ex:
$shopid1, $shopname1 etc
$shopid2, $shopname2 etc

then do


$shops = $DB_site->query("SELECT * FROM itemshops DESC LIMIT 3");
while ($shop = $DB_site->fetch_array($shops)) {
print "<tr><td>$shop[shop_id]</td><td>$shop[shop_name]</td></tr>"
}


this will print table rows with the shop id and shop name in the td's until the DESC LIMIT is reached or the end of your db

Graphics
02-04-2003, 03:16 PM
Thanks man you rule! :)