Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Resource id#3 problem - can't see the cause
Old 02-10-2006, 05:50 AM Resource id#3 problem - can't see the cause
Skilled Talker

Posts: 82
Trades: 0
Hi,

I'm trying to populate a pulldown menu with values from a database field which are separated by commas. It's a simple enough thing to do, so I am a little baffled as to why I'm not getting the result I'm after!

Instead of a nicely populated menu, I get the word "Array" and, when I echo out all of the variables, I get "resource id#3".

Here's the code:

PHP Code:
include ("dbconnect.inc.php");
$product_id=$_GET['product_id'];
$query="SELECT quantity FROM products";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
$delimited=$result;
$delimiter=",";
$splitcontents explode($delimiter$delimited); 
 
 
echo 
"<select name=\"quantity\">";
while (
$i $num){
echo 
"<option>$splitcontents</option>";
$i++;
}
echo 
"</select>";
echo (
"$splitcontents");
echo (
"$delimited");
echo (
"$result"); 
Can anyone shed some light on this?

It's probably just something simple, as usual!

Thanks!
mattcooper is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-10-2006, 06:19 AM Re: Resource id#3 problem - can't see the cause
neroux's Avatar
Ultra Talker

Posts: 284
Trades: 0
mysql_query() does not return any actual data, but only a handle to it. Then you can access the data with the mysql_fetch_* functions.

Below is a version which should work.

Code:
<?php
    include ("dbconnect.inc.php");
    $product_id=$_GET['product_id'];
    $query="SELECT quantity FROM products";
    $result=mysql_query($query);
?>
 
<select name="quantity">
    <?php while ($row=mysql_fetch_row($result)) :?>
    <option><?php echo $row[0]; ?></option>
    <?php endwhile; ?>
</select>
 
<?php 
    echo $splitcontents;
    echo $delimited;
    echo $result;
?>
__________________

Please login or register to view this content. Registration is FREE
- The world at your fingertips
• Share your city with the world

--
Please login or register to view this content. Registration is FREE
neroux is offline
Reply With Quote
View Public Profile
 
Old 02-10-2006, 07:03 AM Re: Resource id#3 problem - can't see the cause
Skilled Talker

Posts: 82
Trades: 0
Hi Neroux, you always come to my rescue! Thanks...

OK, your code now results in a value in the menu, so that's a step forward. The result of the query, however, needs to be split up and echoed separately - i.e instead of 250,500,750,1000 appearing as one value, they should each be a separate option. So, I've adjusted the code as follows and I'm still just getting the word "Array" in the menu.

PHP Code:
include ("dbconnect.inc.php");
$product_id=$_GET['product_id'];
$query="SELECT quantity FROM products";
$result=mysql_query($query);
mysql_close();
echo 
"<select name=\"quantity\">";
while (
$row=mysql_fetch_row($result)) :
$options explode(","$row); 
echo 
"<option>";
echo 
$options[0];
echo 
"</option>";
endwhile;
echo 
"</select>"
Any ideas? Thanks again...
mattcooper is offline
Reply With Quote
View Public Profile
 
Old 02-10-2006, 07:07 AM Re: Resource id#3 problem - can't see the cause
neroux's Avatar
Ultra Talker

Posts: 284
Trades: 0
What does the quantity column of your products table contain for each row?
__________________

Please login or register to view this content. Registration is FREE
- The world at your fingertips
• Share your city with the world

--
Please login or register to view this content. Registration is FREE
neroux is offline
Reply With Quote
View Public Profile
 
Old 02-10-2006, 07:10 AM Re: Resource id#3 problem - can't see the cause
Skilled Talker

Posts: 82
Trades: 0
Just some comma separated values to be split up and used as options. Currently, in the test database, the values are 250,500.
mattcooper is offline
Reply With Quote
View Public Profile
 
Old 02-10-2006, 07:15 AM Re: Resource id#3 problem - can't see the cause
neroux's Avatar
Ultra Talker

Posts: 284
Trades: 0
And you always want to display just the first value?

Then the following should work
PHP Code:
include ("dbconnect.inc.php");
 
$product_id=$_GET['product_id'];
 
$query="SELECT quantity FROM products";
$result=mysql_query($query);
 
echo 
"<select name=\"quantity\">";
while (
$row=mysql_fetch_row($result))
{
    
$options explode(','$row[0]);
    echo 
"<option>";
    echo 
$options[0];
    echo 
"</option>";
}
echo 
"</select>";
 
mysql_close(); 
__________________

Please login or register to view this content. Registration is FREE
- The world at your fingertips
• Share your city with the world

--
Please login or register to view this content. Registration is FREE

Last edited by neroux; 02-10-2006 at 07:16 AM..
neroux is offline
Reply With Quote
View Public Profile
 
Old 02-10-2006, 07:20 AM Re: Resource id#3 problem - can't see the cause
Skilled Talker

Posts: 82
Trades: 0
He heh, we're almost there... but I want all of the values to display, hence the CSV format. This code works fine but, as you said, only displays the first value.
mattcooper is offline
Reply With Quote
View Public Profile
 
Old 02-10-2006, 07:28 AM Re: Resource id#3 problem - can't see the cause
neroux's Avatar
Ultra Talker

Posts: 284
Trades: 0
So you want to display all individual separated values from all rows?

Why dont you just store them in separate rows?
__________________

Please login or register to view this content. Registration is FREE
- The world at your fingertips
• Share your city with the world

--
Please login or register to view this content. Registration is FREE

Last edited by neroux; 02-10-2006 at 07:30 AM..
neroux is offline
Reply With Quote
View Public Profile
 
Old 02-10-2006, 07:32 AM Re: Resource id#3 problem - can't see the cause
neroux's Avatar
Ultra Talker

Posts: 284
Trades: 0
Then this should work
PHP Code:
include ("dbconnect.inc.php");

$product_id=$_GET['product_id'];

$query="SELECT quantity FROM products";
$result=mysql_query($query);

echo 
"<select name=\"quantity\">";
while (
$row=mysql_fetch_row($result))
{
    
$vals explode(','$row[0]);
    foreach (
$vals as $val)
    {
        echo 
"<option>";
        echo 
$val;
        echo 
"</option>";
    }
}
echo 
"</select>";
 
mysql_close(); 
__________________

Please login or register to view this content. Registration is FREE
- The world at your fingertips
• Share your city with the world

--
Please login or register to view this content. Registration is FREE

Last edited by neroux; 02-10-2006 at 07:34 AM..
neroux is offline
Reply With Quote
View Public Profile
 
Old 02-10-2006, 07:33 AM Re: Resource id#3 problem - can't see the cause
Skilled Talker

Posts: 82
Trades: 0
Because I'm coding an e-commerce facility (my first, as you may gather!), and the "quantity" row can contain any number of values. If I only provide, say, five rows and I have six quantities to enter, I'm stuck. Make sense?
mattcooper is offline
Reply With Quote
View Public Profile
 
Old 02-10-2006, 07:35 AM Re: Resource id#3 problem - can't see the cause
neroux's Avatar
Ultra Talker

Posts: 284
Trades: 0
Okay, does the above solution work for you?
__________________

Please login or register to view this content. Registration is FREE
- The world at your fingertips
• Share your city with the world

--
Please login or register to view this content. Registration is FREE
neroux is offline
Reply With Quote
View Public Profile
 
Old 02-10-2006, 07:35 AM Re: Resource id#3 problem - can't see the cause
Skilled Talker

Posts: 82
Trades: 0
Spot on. You're a Godsend today, I was stuck on this quite badly! Another lesson learned.

Thanks fella!
mattcooper is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Resource id#3 problem - can't see the cause
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.60467 seconds with 12 queries