RE: [SMARTY] Assigning mysql query to smarty template
| From: | James Puddicombe | Date: | Tue, 10 Sep 2002 22:17:37 +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-5958@lists.php.net to get a copy of this message | ||
I believe you have a syntax error in the line:
{foreach from=$dbresult item=$current key=$ISBN}
in that the reference to '$current' should not have a prefix of '$'.
James
-----Original Message-----
From: gawan [mailto:mail@gawan.net]
Sent: 09 September 2002 00:47
To: smarty-general@lists.php.net
Subject: Re: [SMARTY] Assigning mysql query to smarty template
I tried writing host, user and password directly to the mysql_pconnenct
function. It did not help. There is no typo in where calusus, I am comparing
a larger table against a sample table, both have similar primary keys that
should match (P_ISBN in Product and isbn in Sample). But one other typo I
found: "order by P_ISBN" should be "order by ISBN". That did not help
though)
Also in fact the error appeared also when removing the template code, I just
did not notice it in a darker upper edge on my page :(
After correcting the order by syntax, mysql_fetch_object(): ... error
disappeared, but now I got a template parsing error:
Parse error: parse error, unexpected T_STRING, expecting ']' in ... this
line:
<?php if (count((array)$this->_tpl_vars['dbresult'])):
foreach ((array)$this->_tpl_vars['dbresult'] as
$this->_tpl_vars['$this->_tpl_vars['ISBN']'] =>
$this->_tpl_vars['$this->_tpl_vars['current']']):
?>
Does the use of item=$current and key=$ISBN seem OK?
thanks
----- Original Message -----
From: "Josh Johnson" <mailing_lists@allusiveimage.com>
To: "'gawan'" <mail@gawan.net>; <smarty-general@lists.php.net>
Sent: Monday, September 09, 2002 1:20 AM
Subject: RE: [SMARTY] Assigning mysql query to smarty template
> The first thing that comes to mind is maybe the scope of your $host,
> $user, etc vars is outside of your template... maybe in a file that
> isn't being included before smarty is called... Or maybe "where P_ISBN =
> isbn" in your query is a typo? Usually mysql_fetch_object will give that
> error when the result id ($result) is no good, usually because the query
> had a syntax error or the connection failed, which would be the case in
> any of the other possible situations I mentioned above.
>
> -- Josh
>
> -----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
>
>