RE: [SMARTY] Assigning mysql query to smarty template

From: Date: Mon, 09 Sep 2002 00:14:09 +0000
Subject: RE: [SMARTY] Assigning mysql query to smarty template
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-5953@lists.php.net to get a copy of this message
One general tip when dealing with mysql queries is to ALWAYS add an 'or die' statement to your query. The message 'not a valid result resource' often suggests that your query failed, and thus did not return a valid result object. Eg: $result = mysql_query("SELECT...") or die (mysql_error()); Secondly, you might want to rewrite your code somewhat (unless I'm misunderstanding your code/intent). $resarray[$row->ISBN] doesn't do anything. You're simply making a reference to an index in the $resarray. Instead what you probably want is: $resarray[$row-ISBN] = $row; OR array_push($resarray,$row); hope it helps.. -mark > -----Original Message----- > From: gawan [mailto:mail@gawan.net] > Sent: Sunday, September 08, 2002 5:58 PM > To: smarty-general@lists.php.net > Subject: Re: [SMARTY] Assigning mysql query to smarty template > > Returning to my question: I have the following code, but I can't seem to > get > it to work. > I got error saying "mysql_fetch_object(): supplied argument is not a valid > MySQL result resource". > > But when I comment out the {foreach} loop in the template, no errors > occur. > So the trouble seems to be in the template inspite of what the message > says. > I don't quite understand what to put in the item and key attributes in the > foreach block. > > Any further advice appreciated. > > gawan > > ---- in my php code ---- > > $conn = mysql_pconnect("$host", "$user", "$passwd"); > mysql_select_db("shop", $conn); > > $query = ("select > P_ISBN as ISBN, > DistinctiveTitle as Title, > PublisherName as Publisher, > AvailabilityCode as Availability, > PriceAmount, > DiscountPercent, > PriceFrom, > PriceEuro > from Product, Sample > where P_ISBN = isbn order by P_ISBN"); > > $result = mysql_query($query, $conn); > > $resarray = array(); > while ($row = mysql_fetch_object($result)) > { > $resarray[$row->ISBN]; > } > > $smarty->assign("dbresult", $resarray); > > $smarty->display("sample.tpl"); > > > ---- then in the template ----- > > {foreach from=$dbresult item=$current key=$ISBN} > <tr> > <td>{$current->ISBN}</td> > <td>{$current->Title}</td> > <td>{$current->Publisher}</td> > <td>{$current->Availability}</td> > <td>{$current->PriceAmount}</td> > <td>{$current->DiscountPercent}</td> > <td>{$current->PriceFrom}</td> > <td>{$current->PriceEuro}</td> > </tr> > {/foreach} > > --------------------------- > > ----- Original Message ----- > From: "Josh Johnson" <mailing_lists@allusiveimage.com> > To: "'gawan'" <mail@gawan.net>; <smarty-general@lists.php.net> > Sent: Sunday, September 01, 2002 7:43 PM > Subject: RE: [SMARTY] Assigning mysql query to smarty template > > > > In the past, I've assigned an entire array to a template variable, and > > so far it's worked great (the templated part of the site has gotten over > > 30,000 unique accesses in the past week). I made an array of > > mysql_fetch_object generated objects, which I think made the data more > > accessable in the template. > > > > I'd do something like this (inside of a plugin function, but you could > > do it inside of the php part of your code, or in a {php} tag): > > > > ... > > mysql query code that assigns the result id to $mysql_result_id > > ... > > > > $result = Array(); //keeps the foreach below from breaking > > while($row = mysql_fetch_object($mysql_result_id)){ > > $result[$row->id] = $row; // I used the id for > > // the key of the array, > > // you could use whatever > > // fielf you want > > } > > > > $smarty->assign("database_result", $result); // replace $smarty with > > // $this if you're using a > > // {php} block, otherwise > > // this is the name of your > > // smarty object. > > > > .... > > > > Then, in the template, I can loop through the returned objects like so: > > {foreach from=$database_result item=current key=$id} > > id: {$current->id} or {$id}<br> > > name: {$current->fname} {$current->lname} > > etc... > > {/foreach} > > > > It might be more efficient though to use mysql_data_seek to pull an > > array or object from the database as needed, instead of all at once, > > especially if you have a really large amount of data that's being > > displayed. Depending on how your query is structured, and when you get > > the array, if a large amount of data is being put into an array, it can > > delay the display of the information, even if some of it is disgarded > > (I've had this problem in the past). With mysql_data_seek, you could out > > spit out the data for the row as you receive it. I haven't tried it yet, > > but if I ever get around to writing a plugin that uses mysql_data_seek, > > I'll post it. > > > > Hope that helps. > > > > -- Josh > > > > -----Original Message----- > > From: gawan [mailto:mail@gawan.net] > > Sent: Sunday, September 01, 2002 12:13 PM > > To: smarty-general@lists.php.net > > Subject: [SMARTY] Assigning mysql query to smarty template > > > > Hi > > > > I am a beginner in using Smarty. Where could I find a basic example on > > how > > to assign a mysql query to a smarty template variable. > > > > Should I try to assign every result row separately or would assigning > > the > > whole query at once be more elegant (as an array of arrays). I am still > > struggling with getting the basics right. > > > > thank you > > > > P.S. I wonder why smarty.php.net is so often unreachable? > > > > > > -- > > Smarty General Mailing List (http://smarty.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > > > -- > > Smarty General Mailing List (http://smarty.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > -- > Smarty General Mailing List (http://smarty.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.smarty.general (#5953) next »