Jump to content

[SOLVED] forms, multiple selections into one field


LordPsyan

Recommended Posts

This is my firs time posting here, so please forgive me if this is in the wrong section, or my format is incorrect.

I searched and searched and could find nothing, but wording the question is probably my problem.

 

I have a simple while look, that displays about 200 items in a database.

 

<?
$query="select * from items";
$rt=mysql_query($query);
echo mysql_error();
echo "<table border=0 align=left width=50%>";
echo "<td align=left width=5% valign=top><form action='items-totals.php' method='get'></td>";
echo "<td align=left width=10% valign=top><b>Price</b></td>";
echo "<td align=left width=20% valign=top><b>item number</b></td>";
echo "<td align=left width=65% valign=top><b>Item Name</b></td></tr><tr>";
while($nt=mysql_fetch_array($rt)){
echo "<td align=left width=5% valign=top><input name='item-$nt[entry]' type='checkbox' value='$nt[name]'></td>
<td align=left width=10% valign=top><span style='font-size: 12px; text-decoration: none;'>$$nt[dollar]</td>
<td align=left width=20 valign=top>$nt[entry]</td>
<td align=left width=65% valign=top><a class='thumbnail' href='#thumb'>$nt[name]<span><img src='images/popup/$nt[entry].png' /></span></a></span></td></tr>";}
echo "<td width=5%></td><td width=10%></td><td width=20%><input name=total type=submit value=submit></form></td><td width=65%></td></tr></table>";
?>

 

This prints out fine, and puts a checkbox next to each item. When I select a few of the items, and click submit, i get an url something like this:

items-totals.php?item-103995=Blade+of+the+Betrayer&item-103997=Betrayer&item-100091=Sceptre+of+the+High+Priest

 

What I am trying to do is have these items displayed on the page. The problem I have is since I do not know what item's they will select, how do I get it to grab all the item-xxxxx and post it onto the page?

 

I can get it to work fine if I manually specify a few of the numbers. example:

 

<?
$item = $_GET["item-103997"];
$item2 = $_GET["item-100091"];
echo "$item<br />"; 
echo "$item2<br />"; 

 

but I do not want to manually type in all of the possibilities, and besides, it would put many breaks on the page, making it look sloppy. This information will also be passed on to another page, once it has been accepted by the person selecting items, and all the choices must be combined into one single entry. It is really hard for me to explain exactly what I want, but if anyone has any idea, it sure would help me tons.

 

Thanks in advance.

 

LP

You should not use the GET method to do this, you need to use POST because the values you are trying to capture will need to be taken as an array. Besides the fact with 200 items you are looking at one hell of a long URL if the user decides to check everything :)

Make the name of the c/box = "item[]" and the value = $nt['entry']

 

echo "<td align=left width=5% valign=top><input name='item[]' type='checkbox' value='{$nt['entry']}'></td>

 

On your next page, to get the selected items

 

<?php
$items = join ("','", $_GET['item']);                                     // or $_POST if post method
$sql = "SELECT * FROM items WHERE entry IN ('$items')";

Make the name of the c/box = "item[]" and the value = $nt['entry']

 

echo "<td align=left width=5% valign=top><input name='item[]' type='checkbox' value='{$nt['entry']}'></td>

 

On your next page, to get the selected items

 

<?php
$items = join ("','", $_GET['item']);                                     // or $_POST if post method
$sql = "SELECT * FROM items WHERE entry IN ('$items')";

 

Works perfectly!

 

You guys are great. Can't figure out the "post" thing tho, to remove the massive URL nothing displays. get works, and most people won't be picking 200 items so I guess it isn't that important. now I am off to the php math section to figure out how to add the selected items together. well not really, each item has a cost in gold (made it simple, and gave it a flat rate of gold, instead of gold copper silver, etc. so math should be simple)

 

i'll post here again if I get confused. PHP is so powerful, but wow its alot to learn :P

 

thanks again.

 

LP

This is all the same part of the script. I got the "echo" to work perfectly. now here is what I am having problem with...

 

Each of the items has a set gold price.

 

I want to total the selected gold prices together, and set that as a variable (like $total)

then I can display the total. Here is the code I have so far, and for some reason it only shows the total for the first field, not first and second

here is my code:

 

$query2 = "SELECT sum(buyprice) as total FROM item_template WHERE entry = '100101' AND '100103'";
$bs=mysql_query($query2);
echo mysql_error();
echo "total cost:<br />";
echo (mysql_result($bs, "total"));

 

but the "AND" is not working. I know this should go into the math section, but it is part of the previous code.

 

entry 100101 = 10 gold

entry 100103 = 15 gold

 

so the total obviously is 25, but it only shows 10.

 

So apparently my syntax is wrong. Please help, I have been googling this for about 8 hours now, and I think google is mad at me. :)

 

once again, thanks in advanced.

 

LP

SELECT SUM(buyprice) as total FROM item_template WHERE entry IN ('100101' ,'100103')

 

which is equivalent to

SELECT SUM(buyprice) as total FROM item_template WHERE (entry = '100101') OR (entry = '100103')

SELECT SUM(buyprice) as total FROM item_template WHERE entry IN ('100101' ,'100103')

 

I own 3 servers, and 4 workstations. the servers are on a rack. On every single server, and all the workstations in firefox I have bookmarked this site. Never before have I had perfect answers to my questions. AND it was super fast.

 

This code worked perfectly, and my script is done. I knew it was a matter of syntax. Thanks a WHOLE bunch.

 

I will try my best to help people on here, and I now have a new home for php questions... just wish people would put good subjects on their posts, so it would be easier to search....

 

Thanks a million

 

LP

 

ps how do I mark this as solved?

P.S.

 

If '100101' ,'100103' are the same selected items as in your earlier checkbox question then

 

SELECT SUM(buyprice) as total FROM item_template WHERE entry IN ('100101' ,'100103')

 

would be

SELECT SUM(buyprice) as total FROM item_template WHERE entry IN ('$items')

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.