Multiple MySQL Queries

Multiple MySQL Queries

am 28.07.2009 16:11:41 von sono-io

This may be more of a MySQL question than PHP, but I'm hoping someone
can point me in the right direction. I have working code (below) that
pulls data from a particular category in our db. I'd like to be able
to pull data from multiple categories in the same db and place them on
the same page.

I've been working on this for the last 4 days with no luck. Some
pages say that you can't do multiple MySQL queries while others say
there is a workaround but I haven't found it. Does anyone know how to
do this?

Thanks,
Frank

--------

// in header

$item_list = "";
$cat1 = "01100-01200-01300-06403";
$result = mysql_query("SELECT itemid,description,unitprice FROM
catalog WHERE categories='" . $cat1 . "' ORDER BY itemid",$db);

while ($item = mysql_fetch_assoc($result))
{
$item_list .= "" . $item['itemid'] . "
$item['itemid'] . "=1\">". $item['description'] ."
$". money_format('%i', $item['unitprice']) ."
";
}
echo "

$item_list
Item IDDescription
>(Click for more info)
Price Each th>Purchase
";
?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 28.07.2009 16:16:57 von Phpster

On Tue, Jul 28, 2009 at 10:11 AM, wrote:
> =A0 =A0 =A0 =A0This may be more of a MySQL question than PHP, but I'm hop=
ing someone
> can point me in the right direction. =A0I have working code (below) that =
pulls
> data from a particular category in our db. =A0I'd like to be able to pull=
data
> from multiple categories in the same db and place them on the same page.
>
> =A0 =A0 =A0 =A0I've been working on this for the last 4 days with no luck=
.. =A0Some
> pages say that you can't do multiple MySQL queries while others say there=
is
> a workaround but I haven't found it. =A0Does anyone know how to do this?
>
> Thanks,
> Frank
>
> --------
>
> // in header
>
> > =A0 =A0 =A0 =A0$item_list =3D "";
> =A0 =A0 =A0 =A0$cat1 =3D "01100-01200-01300-06403";
> =A0 =A0 =A0 =A0$result =3D mysql_query("SELECT itemid,description,unitpri=
ce FROM
> catalog WHERE categories=3D'" . $cat1 . "' =A0ORDER BY itemid",$db);
>
> while ($item =3D mysql_fetch_assoc($result))
> {
> $item_list .=3D "" . $item['itemid'] . "
> =A0 =A0 =A0 =A0 .
> $item['itemid'] . "=3D1\">". $item['description'] ."

> =A0 =A0 =A0 =A0$". money_format('%i', $item['unitpr=
ice'])
> ."
> =A0 =A0 =A0 =A0";
> }
> echo "

$item_list
Item IDDescription
t
> size=3D\"-1\">(Click for more info)
Price
> Each
Purchase
";
> ?>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

change it to an IN clause for the categories

$result =3D mysql_query("SELECT itemid,description,unitprice FROM
catalog WHERE categories in('$cat1',"$cat2',$cat3','$catN') ORDER BY
itemid",$db);


--=20

Bastien

Cat, the other other white meat

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 28.07.2009 18:32:03 von List Manager

Bastien Koert wrote:
> On Tue, Jul 28, 2009 at 10:11 AM, wrote:
>> This may be more of a MySQL question than PHP, but I'm hoping someone
>> can point me in the right direction. I have working code (below) that pulls
>> data from a particular category in our db. I'd like to be able to pull data
>> from multiple categories in the same db and place them on the same page.
>>
>> I've been working on this for the last 4 days with no luck. Some
>> pages say that you can't do multiple MySQL queries while others say there is
>> a workaround but I haven't found it. Does anyone know how to do this?
>>
>> Thanks,
>> Frank
>>
>> --------
>>
>> // in header
>>
>> >> $item_list = "";
>> $cat1 = "01100-01200-01300-06403";
>> $result = mysql_query("SELECT itemid,description,unitprice FROM
>> catalog WHERE categories='" . $cat1 . "' ORDER BY itemid",$db);
>>
>> while ($item = mysql_fetch_assoc($result))
>> {
>> $item_list .= "" . $item['itemid'] . "
>> >> $item['itemid'] . "=1\">". $item['description'] ."
>> $". money_format('%i', $item['unitprice'])
>> ."
>> ";
>> }
>> echo "

$item_list
Item IDDescription
>> size=\"-1\">(Click for more info)
Price
>> Each
Purchase
";
>> ?>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
> change it to an IN clause for the categories
>
> $result = mysql_query("SELECT itemid,description,unitprice FROM
> catalog WHERE categories in('$cat1',"$cat2',$cat3','$catN') ORDER BY
> itemid",$db);
>
>

Or if you want the results in separate "tables", do this.

// in header
$item_list = "";
$cats = array('01100', '01200', '01300', '06403');

foreach ( $cats AS $cat ) {
$cat = mysql_real_escape_string($cat, $db);
$SQL = "SELECT itemid,
description,
unitprice
FROM catalog
WHERE categories='{$cat}"'
ORDER BY itemid";
;

if ( ( $results = mysql_query($SQL, $db) !== false ) {
echo <<






HTML;
while ( $item = mysql_fetch_assoc($result) ) {
$price = money_format('%i', $item['unitprice']);
echo <<





ROW;
}
echo '
Item ID Description

(Click for more info)
Price Each Purchase
{$item['itemid']}
{$item['description']}
{$price}
';
} else {
echo "No results for category #{$cat}!";
}
}

?>



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 28.07.2009 20:22:36 von List Manager

sono-io@fannullone.us wrote:
> Hi Jim,
>
>> Take a look inside your money_format() function look for the problem.
>
> I apologize for replying too quickly.
>
> The money_format() function looks just like the one I had before, so
> that's fine.
>
> Upon closer inspection, I found a few problems:
>
> 1] WHERE categories='{$cat}"'
> should be
> WHERE categories='{$cat}'

Sorry, my bad

>
> 2] if ( ( $results = mysql_query($SQL, $db) !== false ) {
> should be
> if ( $results = mysql_query($SQL, $db) !== false ) {
>

But, you do want the first line, expect with the closing bracket.

if ( ( $results = mysql_query($SQL, $db) ) !== false ) {

You want to have the inner portion processed then the comparison done.
Not the other way around.

> With those fixed, the page is rendering now, but giving me this error:
> Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL
> result resource
>
> for this line:
> while ( $item = mysql_fetch_assoc($result) ) {
>

Should be
while ( $item = mysql_fetch_assoc($results) ) {

> and the tables are blank. Only the table headers show. So...
> everything except the while statement appears to be working.
>
> I've Googled that error message but can't find the casue. Any help
> would be much appreciated.
>
> Frank

Sorry about the other typo's!

Jim


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 28.07.2009 21:48:35 von List Manager

Please, click "Reply All" so the list may benefit from our talking.

Read below for further information.

sono-io@fannullone.us wrote:
> Jim,
>
>> if ( ( $results = mysql_query($SQL, $db) ) !== false ) {
>>
>> You want to have the inner portion processed then the comparison done.
>> Not the other way around.
>
> Of course, that makes sense. =:\
>
>> Sorry about the other typo's!
>
> No problem. That just helps to keep me on my toes! =;)
>
> If I may bother you for a little more help. How would I go about
> making the 2nd category display in a column in the same table as the 1st
> category, instead of a separate table, i.e.
>
> instead of displaying the products like this:
>
> itemid
> 14367C4
> 14487C4
> 14547C4
> 18247C4
> A14367C4
> A14487C4
> A14547C4
> A18247C4
> ...
>
> they would be displayed side by side in a single table like this:
>
> itemid itemid
> 14367C4 A14367C4
> 14487C4 A14487C4
> 14547C4 A14547C4
> 18247C4 A18247C4
> ...
>
> or am I asking too much now?
>
> Thanks again. I really do appreciate your help. I'm so much closer
> than I ever would have been on my own!
>
> Frank
>
> --------------------------
>
> FYI Here's the full code that works:
>
> > $item_list = "";
> $cats = array('01100-01200-01300-06403', '01100-02201-01300-06403');
>
> foreach ( $cats AS $cat ) {
> $cat = mysql_real_escape_string($cat, $db);
> $SQL = "SELECT itemid,description,unitprice
> FROM catalog
> WHERE categories='$cat'
> ORDER BY itemid";
>
> if ( ($result = mysql_query($SQL, $db)) !== false ) {
> echo << >


>
>
>
>
>
>
> HTML;
> while ( $item = mysql_fetch_assoc($result) ) {
> $price = money_format('%i', $item['unitprice']);
> echo << >
>
>
>
>
>
>
> ROW;
> }
> echo '
Item IDDescription

> (Click for more info)
Price EachPurchase
{$item['itemid']}
> {$item['description']}
{$price}

> > name="addToCartButton" alt="Add To Cart" />
> > value="{$item['itemid']}" />
> > size="4" />
>
>
';
> } else {
> echo "No results for category #{$cat}!";
> }
> }
>
> ?>


$item_list = "";
$cats = array('01100-01200-01300-06403', '01100-02201-01300-06403');

echo '';
echo '';
foreach ( $cats AS $cat ) {
echo '';
}
echo '';
foreach ( $cats AS $cat ) {
echo '';
}
echo '
'.htmlspecialchars($cat).'
';
$cat = mysql_real_escape_string($cat, $db);
$SQL = "SELECT itemid,description,unitprice
FROM catalog
WHERE categories='$cat'
ORDER BY itemid";

if ( ($result = mysql_query($SQL, $db)) !== false ) {
while ( $item = mysql_fetch_assoc($result) ) {
$price = money_format('%i', $item['unitprice']);
echo <<
>{$item['description']}

ROW;
}
} else {
echo "No results for category #{$cat}!";
}
echo '
';

?>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 28.07.2009 22:54:26 von sono-io

On Jul 28, 2009, at 12:48 PM, Jim Lucas wrote:

> > $item_list = "";
> $cats = array('01100-01200-01300-06403', '01100-02201-01300-06403');
>
> echo '

';
> echo '';
> foreach ( $cats AS $cat ) {
> echo '';
> }
> echo '';
> foreach ( $cats AS $cat ) {
> echo '';
> }
> echo '
'.htmlspecialchars($cat).'
';
> $cat = mysql_real_escape_string($cat, $db);
> $SQL = "SELECT itemid,description,unitprice
> FROM catalog
> WHERE categories='$cat'
> ORDER BY itemid";
>
> if ( ($result = mysql_query($SQL, $db)) !== false ) {
> while ( $item = mysql_fetch_assoc($result) ) {
> $price = money_format('%i', $item['unitprice']);
> echo << >
> > >{$item['description']}
>
> ROW;
> }
> } else {
> echo "No results for category #{$cat}!";
> }
> echo '
';
>
> ?>

We're getting close! This now displays everything in 2 columns.
Ultimately, what I need is a display like this:

Starter Units Add-On Units

Item# Description Price Item# Description Price
18247C4 -------- $85.89 A18247C4 ------- $76.32
18367C4 -------- $97.37 A18367C4 ------- $82.55

I got the headers to work with this code:

....
foreach ( $cats AS $cat ) {
echo 'Item ID';
echo 'Description
';
echo '(Click for more info)';
echo 'Price Each';
echo 'Purchase';
}
echo '';
....

(I don't need to show the category #'s in the header fields) but I
can't get the rest of the data to flow like I want it.

Thanks,
Frank

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 28.07.2009 23:38:16 von sono-io

On Jul 28, 2009, at 12:48 PM, Jim Lucas wrote:

>> >> $item_list = "";
>> $cats = array('01100-01200-01300-06403', '01100-02201-01300-06403');
>>
>> echo '

';
>> echo '';
>> foreach ( $cats AS $cat ) {
>> echo '';
>> }
>> echo '';
>> foreach ( $cats AS $cat ) {
>> echo '';
>> }
>> echo '
'.htmlspecialchars($cat).'
';
>> $cat = mysql_real_escape_string($cat, $db);
>> $SQL = "SELECT itemid,description,unitprice
>> FROM catalog
>> WHERE categories='$cat'
>> ORDER BY itemid";
>>
>> if ( ($result = mysql_query($SQL, $db)) !== false ) {
>> while ( $item = mysql_fetch_assoc($result) ) {
>> $price = money_format('%i', $item['unitprice']);
>> echo << >>
>> >> >{$item['description']}
>>
>> ROW;
>> }
>> } else {
>> echo "No results for category #{$cat}!";
>> }
>> echo '
';
>>
>> ?>
>
> We're getting close! This now displays everything in 2 columns.
> Ultimately, what I need is a display like this:
>
> Starter Units Add-On Units
>
> Item# Description Price Item# Description Price
> 18247C4 -------- $85.89 A18247C4 ------- $76.32
> 18367C4 -------- $97.37 A18367C4 ------- $82.55

I just noticed something. This code places all items in a single
block. Each item needs to be separated, so using my example
data above the html output would need to look like this:


Starter Units
Add-On Units


Item#
Description
Price
Item#
Description
Price


18247C4 <-- starter unit info
-------- " "
$85.89 " "
A18247C4 <-- add-on unit info
------- " "
$76.32 " "


18367C4 <-- starter unit info
-------- " "
$97.37 " "
A18367C4 <-- add-on unit info
------- " "
$82.55 " "


Thanks,
Frank

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 29.07.2009 01:38:39 von List Manager

sono-io@fannullone.us wrote:
>
> On Jul 28, 2009, at 12:48 PM, Jim Lucas wrote:
>
>> >> $item_list = "";
>> $cats = array('01100-01200-01300-06403', '01100-02201-01300-06403');
>>
>> echo '

';
>> echo '';
>> foreach ( $cats AS $cat ) {
>> echo '';
>> }
>> echo '';
>> foreach ( $cats AS $cat ) {
>> echo '';
>> }
>> echo '
'.htmlspecialchars($cat).'
';
>> $cat = mysql_real_escape_string($cat, $db);
>> $SQL = "SELECT itemid,description,unitprice
>> FROM catalog
>> WHERE categories='$cat'
>> ORDER BY itemid";
>>
>> if ( ($result = mysql_query($SQL, $db)) !== false ) {
>> while ( $item = mysql_fetch_assoc($result) ) {
>> $price = money_format('%i', $item['unitprice']);
>> echo << >>
>> >> >{$item['description']}
>>
>> ROW;
>> }
>> } else {
>> echo "No results for category #{$cat}!";
>> }
>> echo '
';
>>
>> ?>
>
> We're getting close! This now displays everything in 2 columns.
> Ultimately, what I need is a display like this:
>
> Starter Units Add-On Units
>
> Item# Description Price Item# Description Price
> 18247C4 -------- $85.89 A18247C4 ------- $76.32
> 18367C4 -------- $97.37 A18367C4 ------- $82.55
>
> I got the headers to work with this code:
>
> ...
> foreach ( $cats AS $cat ) {
> echo 'Item ID';
> echo 'Description
';
> echo '(Click for more info)';
> echo 'Price Each';
> echo 'Purchase';
> }
> echo '';
> ...
>
> (I don't need to show the category #'s in the header fields) but I can't
> get the rest of the data to flow like I want it.
>
> Thanks,
> Frank
>

After Thought!

I saw your other email before sending. The problem with the way you
show you want it there is that each result set would have to be the same
size. I'm going to assume that they won't be, so my solution below show
take care of it.


....


Well, this makes things a little more complicated.

But, here is another round...

$item_list = "";
$cats = array('01100-01200-01300-06403', '01100-02201-01300-06403');

echo '';
echo '';
foreach ( $cats AS $cat ) {
echo <<
';
}
echo '








START;

$cat = mysql_real_escape_string($cat, $db);
$SQL = "SELECT itemid,description,unitprice
FROM catalog
WHERE categories='$cat'
ORDER BY itemid";

if ( ($result = mysql_query($SQL, $db)) !== false ) {
while ( $item = mysql_fetch_assoc($result) ) {
$price = money_format('%i', $item['unitprice']);
echo <<






ROW;
}

} else {
echo "No results for category #{$cat}!";
}
echo '
Item# Description Price
{$item['itemid']} {$item['description']} {$item['price']}
';

?>


I think all the above is correct. Give it a try and let us know.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 29.07.2009 05:12:27 von sono-io

On Jul 28, 2009, at 4:38 PM, Jim Lucas wrote:

> I saw your other email before sending. The problem with the way you
> show you want it there is that each result set would have to be the
> same
> size. I'm going to assume that they won't be...

Well, they SHOULD be but you never know. Thanks for thinking about
that.

> But, here is another round...
>
> I think all the above is correct. Give it a try and let us know.

It works! The only thing I had to change was:

{$item['price']}
to
{$price}

Jim, I can't thank you enough. This is exactly what I was hoping for.

Regards,
Frank

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 03.08.2009 23:31:58 von sono-io

I'd like to revisit this one last time. Below is the revised code
I'm using, but I don't like having individual 'Add To Cart' buttons
for each item. I've tried to find a way to have only one that resides
outside the nested tables for all items, but without luck. It would
need to send every item that has a quantity to the cart, but only
those items. The URL that would be passed needs to look like this:

/shop.cgi?
c=viewcart.htm&itemid=P74S&i_P74S=80&S2448S&i_S2448S=100&AC& i_AC=26

(The above URL is sending 3 items and quantities to the cart; 80 -
P74S, 100 - S2448S and 26 - AC.)

Thanks again for all your help. I'm learning a lot on this list -
albeit slowly! ;)

Regards,
Frank

------------------------

$cats = array('02121-19222-13349-11451' => 'Stationary
Posts','04103-99222-48340-11422' => 'Solid Galvanized Shelves');

echo '

';

foreach ( $cats AS $cat => $title) {
echo <<
';
}
echo '










START;

$cat = mysql_real_escape_string($cat, $db); //sanitizes the data
to prevent SQL injection
$SQL = "SELECT itemid,description,unitprice
FROM catalog
WHERE categories='$cat'
ORDER BY itemid";

if ( ($result = mysql_query($SQL, $db)) !== false ) {
while ( $item = mysql_fetch_assoc($result) ) {
$price = "$" . number_format($item['unitprice'],2);
echo <<







ROW;
}

} else {
echo "No results for category #{$cat}!";
}
echo '
{$title}
Item# Description

(Click for more info)
Price Qty
{$item['itemid']} {$item['description'] } {$price}

value="{$item['itemid']}" />
size="4" />

name="addToCartButton" alt="Add To Cart" />

';
?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 04.08.2009 17:40:58 von sono-io

Is what I'm looking to do not possible in PHP? I've come close with
a JavaScript version, but I'd rather not rely on everyone having JS
turned on. Or am I stuck having to use JS?

Thanks again,
Frank


> I'd like to revisit this one last time. Below is the revised code
> I'm using, but I don't like having individual 'Add To Cart' buttons
> for each item. I've tried to find a way to have only one that
> resides outside the nested tables for all items, but without luck.
> It would need to send every item that has a quantity to the cart,
> but only those items. The URL that would be passed needs to look
> like this:
>
> /shop.cgi?
> c=viewcart.htm&itemid=P74S&i_P74S=80&S2448S&i_S2448S=100&AC& i_AC=26
>
> (The above URL is sending 3 items and their quantities to the cart;
> 80 - P74S, 100 - S2448S and 26 - AC.)
>
> Thanks again for all your help. I'm learning a lot on this list -
> albeit slowly! ;)
>
> Regards,
> Frank
>
> ------------------------
>
> > $cats = array('02121-19222-13349-11451' => 'Stationary
> Posts','04103-99222-48340-11422' => 'Solid Galvanized Shelves');
>
> echo '

';
>
> foreach ( $cats AS $cat => $title) {
> echo << >
>
';
> }
> echo '

>
>
>
>
>
>
>
>
>
> START;
>
> $cat = mysql_real_escape_string($cat, $db);
> $SQL = "SELECT itemid,description,unitprice
> FROM catalog
> WHERE categories='$cat'
> ORDER BY itemid";
>
> if ( ($result = mysql_query($SQL, $db)) !== false ) {
> while ( $item = mysql_fetch_assoc($result) ) {
> $price = "$" . number_format($item['unitprice'],2);
> echo << >
>

>
>
>
>
>
>
> ROW;
> }
>
> } else {
> echo "No results for category #{$cat}!";
> }
> echo '
{$title}
Item#Description

> (Click for more info)
PriceQty
{$item['itemid']}{$item['description'] }{$price}
>

> > value="{$item['itemid']}" />
> > size="4" />
>
> > name="addToCartButton" alt="Add To Cart" />
>

>
';
> ?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 04.08.2009 17:47:31 von Phpster

On Tue, Aug 4, 2009 at 11:40 AM, wrote:
> =A0 =A0 =A0 =A0Is what I'm looking to do not possible in PHP? =A0I've com=
e close with
> a JavaScript version, but I'd rather not rely on everyone having JS turne=
d
> on. =A0Or am I stuck having to use JS?
>
> Thanks again,
> Frank
>
>
>> =A0 =A0 =A0 =A0I'd like to revisit this one last time. =A0Below is the r=
evised code
>> I'm using, but I don't like having individual 'Add To Cart' buttons for =
each
>> item. =A0I've tried to find a way to have only one that resides outside =
the
>> nested tables for all items, but without luck. =A0It would need to send =
every
>> item that has a quantity to the cart, but only those items. =A0The URL t=
hat
>> would be passed needs to look like this:
>>
>>
>> /shop.cgi?c=3Dviewcart.htm&itemid=3DP74S&i_P74S=3D80&S2448S& i_S2448S=3D1=
00&AC&i_AC=3D26
>>
>> (The above URL is sending 3 items and their quantities to the cart; 80 -
>> P74S, 100 - S2448S and 26 - AC.)
>>
>> =A0 =A0 =A0 =A0Thanks again for all your help. =A0I'm learning a lot on =
this list -
>> albeit slowly! ;)
>>
>> Regards,
>> Frank
>>
>> ------------------------
>>
>> >> $cats =3D array('02121-19222-13349-11451' =3D> 'Stationary
>> Posts','04103-99222-48340-11422' =3D> 'Solid Galvanized Shelves');
>>
>> echo '

';
>>
>> foreach ( $cats AS $cat =3D> $title) {
>> =A0echo << >>
>>
';
>> }
>> echo '

>> =A0
>> =A0 =A0 =A0
>> =A0 =A0 =A0
>> =A0 =A0 =A0 =A0 =A0
>> =A0 =A0 =A0 =A0 =A0
>> =A0 =A0 =A0 =A0 =A0
>> =A0 =A0 =A0 =A0 =A0
>> =A0 =A0 =A0
>>
>> START;
>>
>> =A0$cat =3D mysql_real_escape_string($cat, $db);
>> =A0$SQL =3D "SELECT itemid,description,unitprice
>> =A0 =A0 =A0 =A0 =A0FROM catalog
>> =A0 =A0 =A0 =A0 =A0WHERE categories=3D'$cat'
>> =A0 =A0 =A0 =A0 =A0ORDER BY itemid";
>>
>> =A0if ( ($result =3D mysql_query($SQL, $db)) !== false ) {
>> =A0 =A0 =A0while ( $item =3D mysql_fetch_assoc($result) ) {
>> =A0 =A0 =A0 =A0 =A0$price =3D "$" . number_format($item['unitprice'],2);
>> =A0 =A0 =A0 =A0 =A0echo << >>
>> =A0

>> =A0 =A0 =A0
>> =A0 =A0 =A0
>> =A0 =A0 =A0
>> =A0 =A0 =A0 =A0 =A0
>> =A0
>>
>> ROW;
>> =A0 =A0 =A0}
>>
>> =A0} else {
>> =A0 =A0 =A0echo "No results for category #{$cat}!";
>> =A0}
>> =A0echo '
{$title}
Item#Descriptio=
n

>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (Click fo=
r more
>> info)
PriceQty
{$item['itemid']} >> href=3D"/shop.cgi?c=3Ddetail.htm&itemid=3D{$item['itemid']}" >{$item['des=
cription']}
{$price}
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0

>> =A0 =A0 =A0 =A0 =A0 =A0 $item['itemid']}" />
>> =A0 =A0 =A0 =A0 =A0 =A0 value=3D"" size=3D"4"
>> />
>> =A0 =A0 =A0 =A0 =A0 =A0 rt.htm" />
>> =A0 =A0 =A0 =A0 =A0 =A0 png"
>> name=3D"addToCartButton" alt=3D"Add To Cart" />
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0

>> =A0 =A0 =A0 =A0
';
>> ?>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

You could do it with PHP, but you would still need to loop through all
the data in the cart to find the element(s) to update. This can be
done via a straight submit button where you send all the data to the
server and the code walks thru the current contents

--=20

Bastien

Cat, the other other white meat

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 04.08.2009 18:43:36 von Jerry Wilborn

--001636af022e7c7d7904705396ce
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Keep in mind that you can use name=var[] value=value1, name=var[]
value=value2 and php will create an array as $_REQUEST['var'] with each of
your values. The keys are numbered and don't count on what order they'll
come through.
Jerry Wilborn
jerrywilborn@gmail.com


On Tue, Aug 4, 2009 at 10:47 AM, Bastien Koert wrote:

> On Tue, Aug 4, 2009 at 11:40 AM, wrote:
> > Is what I'm looking to do not possible in PHP? I've come close
> with
> > a JavaScript version, but I'd rather not rely on everyone having JS
> turned
> > on. Or am I stuck having to use JS?
> >
> > Thanks again,
> > Frank
> >
> >
> >> I'd like to revisit this one last time. Below is the revised
> code
> >> I'm using, but I don't like having individual 'Add To Cart' buttons for
> each
> >> item. I've tried to find a way to have only one that resides outside
> the
> >> nested tables for all items, but without luck. It would need to send
> every
> >> item that has a quantity to the cart, but only those items. The URL
> that
> >> would be passed needs to look like this:
> >>
> >>
> >>
> /shop.cgi?c=viewcart.htm&itemid=P74S&i_P74S=80&S2448S&i_S244 8S=100&AC&i_AC=26
> >>
> >> (The above URL is sending 3 items and their quantities to the cart; 80 -
> >> P74S, 100 - S2448S and 26 - AC.)
> >>
> >> Thanks again for all your help. I'm learning a lot on this list
> -
> >> albeit slowly! ;)
> >>
> >> Regards,
> >> Frank
> >>
> >> ------------------------
> >>
> >> > >> $cats = array('02121-19222-13349-11451' => 'Stationary
> >> Posts','04103-99222-48340-11422' => 'Solid Galvanized Shelves');
> >>
> >> echo '

';
> >>
> >> foreach ( $cats AS $cat => $title) {
> >> echo << > >>
> >>
';
> >> }
> >> echo '

> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >> START;
> >>
> >> $cat = mysql_real_escape_string($cat, $db);
> >> $SQL = "SELECT itemid,description,unitprice
> >> FROM catalog
> >> WHERE categories='$cat'
> >> ORDER BY itemid";
> >>
> >> if ( ($result = mysql_query($SQL, $db)) !== false ) {
> >> while ( $item = mysql_fetch_assoc($result) ) {
> >> $price = "$" . number_format($item['unitprice'],2);
> >> echo << > >>
> >>

> >>
> >>
> >>
> >>
> >>
> >>
> >> ROW;
> >> }
> >>
> >> } else {
> >> echo "No results for category #{$cat}!";
> >> }
> >> echo '
{$title}
Item#Description

> >> (Click for more
> >> info)
PriceQty
{$item['itemid']} > >>
> href="/shop.cgi?c=detail.htm&itemid={$item['itemid']}">{$ite m['description']}
{$price}
> >>

> >> > />
> >> > size="4"
> >> />
> >>
> >> > >> name="addToCartButton" alt="Add To Cart" />
> >>

> >>
';
> >> ?>
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
> You could do it with PHP, but you would still need to loop through all
> the data in the cart to find the element(s) to update. This can be
> done via a straight submit button where you send all the data to the
> server and the code walks thru the current contents
>
> --
>
> Bastien
>
> Cat, the other other white meat
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--001636af022e7c7d7904705396ce--

Re: Multiple MySQL Queries

am 04.08.2009 20:50:11 von sono-io

On Aug 4, 2009, at 9:43 AM, Jerry Wilborn wrote:

> Keep in mind that you can use name=var[] value=value1, name=var[]
> value=value2 and php will create an array as $_REQUEST['var'] with
> each of your values. The keys are numbered and don't count on what
> order they'll come through.

Thanks for the tip, Jerry. I'm still trying to figure this out, but
in the meantime, I'm running into another problem. I have the action
on the form set as "/shop.cgi?c=viewcart.htm" but it keeps stripping
out everything after the question mark. I want "c=viewcart.htm" sent
only once in the URL, so I can't place it in the form as it then gets
sent along with every itemid in the form.

How does one append a path extension after the auto-generated
question mark?

Thanks,
Frank

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 04.08.2009 20:56:43 von Jerry Wilborn

--0016368e1dcd9d91350470557251
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

If the form method is "POST" then set the 'c' variable with a hidden value
within the form:
Jerry Wilborn
jerrywilborn@gmail.com


On Tue, Aug 4, 2009 at 1:50 PM, wrote:

>
> On Aug 4, 2009, at 9:43 AM, Jerry Wilborn wrote:
>
> Keep in mind that you can use name=var[] value=value1, name=var[]
>> value=value2 and php will create an array as $_REQUEST['var'] with each of
>> your values. The keys are numbered and don't count on what order they'll
>> come through.
>>
>
> Thanks for the tip, Jerry. I'm still trying to figure this out, but
> in the meantime, I'm running into another problem. I have the action on the
> form set as "/shop.cgi?c=viewcart.htm" but it keeps stripping out everything
> after the question mark. I want "c=viewcart.htm" sent only once in the URL,
> so I can't place it in the form as it then gets sent along with every itemid
> in the form.
>
> How does one append a path extension after the auto-generated
> question mark?
>
> Thanks,
> Frank
>

--0016368e1dcd9d91350470557251--

Re: Multiple MySQL Queries

am 04.08.2009 21:03:52 von sono-io

--Apple-Mail-3-522492709
Content-Type: text/plain;
charset=US-ASCII;
format=flowed;
delsp=yes
Content-Transfer-Encoding: 7bit

Sorry... I'm using GET. I have used the code you supplied below, but
as I mentioned, it gets sent for every itemid in the table. I needs
to be sent only once, and right after the action. That's where I'm
stumped.

Frank

On Aug 4, 2009, at 11:56 AM, Jerry Wilborn wrote:

> If the form method is "POST" then set the 'c' variable with a hidden
> value within the form:
>
> Jerry Wilborn
> jerrywilborn@gmail.com
>
>
> On Tue, Aug 4, 2009 at 1:50 PM, wrote:
>
> On Aug 4, 2009, at 9:43 AM, Jerry Wilborn wrote:
>
> Keep in mind that you can use name=var[] value=value1, name=var[]
> value=value2 and php will create an array as $_REQUEST['var'] with
> each of your values. The keys are numbered and don't count on what
> order they'll come through.
>
> Thanks for the tip, Jerry. I'm still trying to figure this
> out, but in the meantime, I'm running into another problem. I have
> the action on the form set as "/shop.cgi?c=viewcart.htm" but it
> keeps stripping out everything after the question mark. I want
> "c=viewcart.htm" sent only once in the URL, so I can't place it in
> the form as it then gets sent along with every itemid in the form.
>
> How does one append a path extension after the auto-generated
> question mark?
>
> Thanks,
> Frank
>


--Apple-Mail-3-522492709--

Re: Multiple MySQL Queries

am 04.08.2009 21:19:58 von Jerry Wilborn

--00163662e657bccecf047055c556
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

I re-read your original thread, and I think *now* I may know what you're
asking.

Bastien is right, you're not going to be able to selectively send data to
PHP with just HTML. Be careful though, as 'GET' has its
limits. POST has limits as well, but for adding items to a shopping
cart you're unlikely to hit those.

If you want to avoid JS,
you're going to have to send the whole page and then sort out what
needs to be updated on the server.

Jerry Wilborn
jerrywilborn@gmail.com


On Tue, Aug 4, 2009 at 2:03 PM, wrote:

> Sorry... I'm using GET. I have used the code you supplied below, but as
> I mentioned, it gets sent for every itemid in the table. I needs to be sent
> only once, and right after the action. That's where I'm stumped.
>
> Frank
>
> On Aug 4, 2009, at 11:56 AM, Jerry Wilborn wrote:
>
> If the form method is "POST" then set the 'c' variable with a hidden value
> within the form:
> Jerry Wilborn
> jerrywilborn@gmail.com
>
>
> On Tue, Aug 4, 2009 at 1:50 PM, wrote:
>
>>
>> On Aug 4, 2009, at 9:43 AM, Jerry Wilborn wrote:
>>
>> Keep in mind that you can use name=var[] value=value1, name=var[]
>>> value=value2 and php will create an array as $_REQUEST['var'] with each of
>>> your values. The keys are numbered and don't count on what order they'll
>>> come through.
>>>
>>
>> Thanks for the tip, Jerry. I'm still trying to figure this out,
>> but in the meantime, I'm running into another problem. I have the action on
>> the form set as "/shop.cgi?c=viewcart.htm" but it keeps stripping out
>> everything after the question mark. I want "c=viewcart.htm" sent only once
>> in the URL, so I can't place it in the form as it then gets sent along with
>> every itemid in the form.
>>
>> How does one append a path extension after the auto-generated
>> question mark?
>>
>> Thanks,
>> Frank
>>
>
>
>

--00163662e657bccecf047055c556--

Re: Multiple MySQL Queries

am 04.08.2009 21:33:28 von Ben Dunlap

> Sorry... I'm using GET. I have used the code you supplied below,
> but as I mentioned, it gets sent for every itemid in the table. I needs
> to be sent only once, and right after the action. That's where I'm
> stumped.

Hidden form inputs should work with GET or POST -- they're only "hidden" from
being displayed on the page where the form is displayed.

I don't follow what you mean by "it gets sent for every itemid" -- can you post
an example of the query string that's being generated?

Ben

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 04.08.2009 21:51:11 von sono-io

On Aug 4, 2009, at 12:33 PM, Ben Dunlap wrote:

> I don't follow what you mean by "it gets sent for every itemid" --
> can you post
> an example of the query string that's being generated?

Well, do I feel stupid. I thought I had moved it to the correct spot
but I hadn't. When value="viewcart.htm" /> is in the table cell with the itemid and qty
fields, it gets sent along with each itemid and it's qty. However,
when I moved it outside the generated table, it now gets sent only
once in the URL, which is what I needed. I apologize for the error.
I've been working on this for the past week and I think it's taking
its toll on me. =;)

Now I just need to work on what Jerry and Bastien told me, as far as
PHP parsing only the populated qty fields, but I'm afraid that's over
my head. =:\

Thanks,
Frank

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 05.08.2009 04:59:45 von sono-io

I've run up against something else here that I can't find an answer
for. This line of the script:

$cats = array('01100-01200-01300-06403' => 'Starter Units',
'01100-02201-01300-06403' => 'Add-On Units', '01100-99222-11341-18451'
=> 'Extra Shelves');

is supposed to pull all items from each category and gives each table
a heading. Unfortunately, if a product in the db has more than one
category assigned to it, it will not show up in the generated table.
So, if an Extra Shelf has a second category, the categories are stored
as 01100-99222-11341-18451``07108-05253-12341-01451 and if it has 3
categories assigned to it, they're stored like
01100
-99222-11341-18451``07108-05253-12341-01451``07108-01254-393 41-01451,
and so on.

Is there a way that I can tell the array to find all products whose
categories either equal exactly the category I'm giving it or if it
_contains_ that particular category? I've tried using the * wildcard
but that didn't work. I've also tried WHERE categories CONTAINS
'$cat' but that didn't work either.

Any insight into this would be greatly appreciated. If I can't find
a fix for this problem, this whole script is useless to me, as most of
our products have multiple categories and so it will only display a
portion of the products that it should, if any at all.

Thanks a million,
Frank

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Multiple MySQL Queries

am 05.08.2009 06:29:41 von sono-io

Well, it took me all evening to figure it out, but I did. The SQL
statement needed to be changed to
WHERE categories LIKE '%$cat%'
and now it works. I had found an old book on my shelf called "MySQL
and Perl for the Web" and found an example in there that pointed me in
the right direction. I also found a website that gives a concise
description of the SQL commands and syntax:
http://www.1keydata.com/sql/sql-commands.html

So all is well again!

Regards,
Frank

> I've run up against something else here that I can't find an answer
> for. This line of the script:
>
> $cats = array('01100-01200-01300-06403' => 'Starter Units',
> '01100-02201-01300-06403' => 'Add-On Units',
> '01100-99222-11341-18451' => 'Extra Shelves');
>
> is supposed to pull all items from each category and gives each
> table a heading. Unfortunately, if a product in the db has more
> than one category assigned to it, it will not show up in the
> generated table. So, if an Extra Shelf has a second category, the
> categories are stored as
> 01100-99222-11341-18451``07108-05253-12341-01451 and if it has 3
> categories assigned to it, they're stored like
> 01100
> -99222
> -11341-18451``07108-05253-12341-01451``07108-01254-39341-014 51, and
> so on.
>
> Is there a way that I can tell the array to find all products whose
> categories either equal exactly the category I'm giving it or if it
> _contains_ that particular category? I've tried using the *
> wildcard but that didn't work. I've also tried WHERE categories
> CONTAINS '$cat' but that didn't work either.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php