PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
xwinger
Forum Newbie
Posts: 3 Joined: Tue Oct 10, 2006 4:28 pm
Post
by xwinger » Tue Oct 10, 2006 4:35 pm
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Is there any way to pass an array of objects in PHP?
This is the code I have:Code: Select all
<?php
include 'iteminfo.php';
function PopDB($drinkarr)
{
$dbhandle = mysql_connect();
if($dbhandle != FALSE)
{
mysql_select_db('test');
for($i=0; $i < count($drinkarr); $i++)
{
$result = mysql_query("SELECT * FROM mytableWHERE itemcode = " . $drinkarr[i]->getItemCode());
}
}
else
echo 'error\n';
echo mysql_result($result, 0, 'itemcode');
}
?>
The call is simply:
Code: Select all
<?php
include('iteminfo.php');
include ('popudb.php');
$row = 0;
$invhandle = fopen('output.csv', 'r');
$line = 0;
//$itemarr = array();
while(($data = fgetcsv($invhandle, 10000)) !== FALSE)
{
$itemarr[] = new Drink;
$num = count($data);
$itemarr[$row]->setItemCode($data[0]);
$itemarr[$row]->setName($data[1]);
$itemarr[$row]->setDescrip($data[2]);
$itemarr[$row]->setClass($data[3]);
$itemarr[$row]->setDept($data[4]);
$itemarr[$row]->setType($data[5]);
$itemarr[$row]->setCountry($data[6]);
$itemarr[$row]->setSize($data[7]);
$itemarr[$row]->setColor($data[8]);
$itemarr[$row]->setVendor($data[9]);
$itemarr[$row]->setShelf($data[10]);
$itemarr[$row]->setCasePrice($data[11]);
$itemarr[$row]->setCaseQty($data[12]);
$itemarr[$row]->setVineyard($data[13]);
$itemarr[$row]->setVintage($data[14]);
$itemarr[$row]->setProof($data[15]);
$itemarr[$row]->setOnHand($data[16]);
$row++;
}
PopDB($itemarr);
This is the error I get:
Fatal error: Call to a member function on a non-object dtemp/ashes/popudb.php on line 13
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Oct 10, 2006 4:42 pm
please try
Code: Select all
<?php
include 'iteminfo.php';
function PopDB($drinkarr)
{
echo '<fieldset><legend>Debug</legend><pre>$drinkarr: ';
var_dump($drinkarr);
echo "</pre></fieldset>\n";
$dbhandle = mysql_connect();
if($dbhandle != FALSE)
{
mysql_select_db('test');
for($i=0; $i < count($drinkarr); $i++)
{
$result = mysql_query("SELECT * FROM mytableWHERE itemcode = " . $drinkarr[i]->getItemCode());
}
}
else
echo 'error\n';
echo mysql_result($result, 0, 'itemcode');
}
?>and
Code: Select all
<?php
include('iteminfo.php');
include ('popudb.php');
$row = 0;
$invhandle = fopen('output.csv', 'r');
$line = 0;
//$itemarr = array();
while(($data = fgetcsv($invhandle, 10000)) !== FALSE)
{
$itemarr[] = new Drink;
$num = count($data);
$itemarr[$row]->setItemCode($data[0]);
$itemarr[$row]->setName($data[1]);
$itemarr[$row]->setDescrip($data[2]);
$itemarr[$row]->setClass($data[3]);
$itemarr[$row]->setDept($data[4]);
$itemarr[$row]->setType($data[5]);
$itemarr[$row]->setCountry($data[6]);
$itemarr[$row]->setSize($data[7]);
$itemarr[$row]->setColor($data[8]);
$itemarr[$row]->setVendor($data[9]);
$itemarr[$row]->setShelf($data[10]);
$itemarr[$row]->setCasePrice($data[11]);
$itemarr[$row]->setCaseQty($data[12]);
$itemarr[$row]->setVineyard($data[13]);
$itemarr[$row]->setVintage($data[14]);
$itemarr[$row]->setProof($data[15]);
$itemarr[$row]->setOnHand($data[16]);
$row++;
}
echo '<fieldset><legend>Debug</legend><pre>';
echo '$row: ', $row, "\n";
echo 'gettype($itemarr): ', gettype($itemarr), "\n"
echo 'count($itemarr): ', count($itemarr), "\n";
echo "</pre></fieldset>\n";
PopDB($itemarr);
xwinger
Forum Newbie
Posts: 3 Joined: Tue Oct 10, 2006 4:28 pm
Post
by xwinger » Tue Oct 10, 2006 4:48 pm
Debug
$row: 2462
gettype($itemarr): array
count($itemarr): 2462
Followed by
Code: Select all
$drinkarr: array(2462) {
[0]=>
&object(drink)(18) {
["itemcode"]=>
string(8) "itemcode"
["name"]=>
string(4) "name"
["ext_desc"]=>
string(8) "ext_desc"
["drinkclass"]=>
string(5) "class"
["dept"]=>
string(4) "dept"
["type"]=>
string(4) "type"
["country"]=>
string(7) "country"
["size"]=>
NULL
["color"]=>
string(5) "color"
["vendor"]=>
string(6) "vendor"
["shelf"]=>
NULL
["caseprice"]=>
NULL
["case_qty"]=>
NULL
["vineyard"]=>
string(8) "vineyard"
["vintage"]=>
string(7) "vintage"
["proof"]=>
NULL
["onhand"]=>
NULL
["age"]=>
NULL
}
Obviously the second part is significantly longer than that, but that's an example of it.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Oct 10, 2006 4:51 pm
ah
$result = mysql_query("SELECT * FROM mytableWHERE itemcode = " . $drinkarr->getItemCode());
forgot a $ there and a space
Code: Select all
$result = mysql_query("SELECT * FROM mytable WHERE itemcode = " . $drinkarr[$i]->getItemCode());Didn't it warn you about an undefined constant i?
Why do you perform all those querries only to echo one field of the last record?
xwinger
Forum Newbie
Posts: 3 Joined: Tue Oct 10, 2006 4:28 pm
Post
by xwinger » Tue Oct 10, 2006 4:55 pm
Wow. Now don't I feel smart.
No, it didn't warm me about the i. Thanks for your help.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Oct 10, 2006 4:59 pm
The message would have been
Notice: Use of undefined constant i - assumed 'i' in filename on line x
but messages for E_NOTICE seem to disabled on your system.
If this is purely a development server you might want to set error_reporting to E_ALL instead of
E_ALL & ~E_NOTICE
phpinfo() tells you which php.ini you have to edit.