|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
misterd Smarty n00b
Joined: 24 Mar 2007 Posts: 2
|
Posted: Sat Mar 24, 2007 7:21 pm Post subject: how to pass a value from a query to a new query |
|
|
down below is rubbish code i know now
Code: |
// connect to the database
$dbh = DB::connect('mysql://xxx:xxx@localhost/www_xxx_nl');
$dbh->setFetchMode(DB_FETCHMODE_ASSOC);
// we want our results back as associative arrays
$home = $dbh->getAll('SELECT kookboek.*, categorie.*, land.* from kookboek INNER JOIN categorie ON kookboek.categorie_id = categorie.categorie_id INNER JOIN land ON kookboek.land_id = land.land_id');
// assign some content. This would typically come from
// a database or other souce, but we'll use static
// values for the purpose of this example.
$smarty->assign('name',$home);
// connect to the database
$dbh1 = DB::connect('mysql://xxx:xxx@localhost/www_xxx_nl');
$dbh1->setFetchMode(DB_FETCHMODE_ASSOC);
// we want our results back as associative arrays
$home1 = $dbh1->getAll('SELECT ingredienten.*, ingredienten_koppel.* from ingredienten_koppel INNER JOIN ingredienten ON ingredienten.id_ingredient = ingredienten_koppel.id_ingredient WHERE ingredienten_koppel.id=".$row[kookboek.id]."');
// assign some content. This would typically come from
// a database or other souce, but we'll use static
// values for the purpose of this example.
$smarty->assign('name1',$home1);
// display it
$smarty->display('nieuwe_recept.tpl');
|
my Question is why does $row[kookboek.id] doesn't have a value and does it stay blank[/code]
one way to do the above is to include php code in the template like the following example:
Code: |
{php}
$kookboek_result = mysql_query("SELECT kookboek.*, categorie.*, land.* from kookboek INNER JOIN categorie ON kookboek.categorie_id = categorie.categorie_id INNER JOIN land ON kookboek.land_id = land.land_id")
or die(mysql_error());
while ($kookbook_row = mysql_fetch_row($kookboek_result))
{
$gerechtnaam = $kookbook_row[1];
$categorie = $kookbook_row[8];
$land = $kookbook_row[10];
$bereidingswijze = $kookbook_row[4];
$opmerkingen = $kookbook_row[5];
$foto = $kookbook_row[6];
print("<b>gerechtnaam:</b> $gerechtnaam<br>");
print("<b>categorie:</b> $categorie<br>");
print("<b>land:</b> $land<br>");
print("<b>ingredienten:</b><br>");
{
$ingredient_result =
mysql_query("SELECT ingredienten.*, ingredienten_koppel.* from ingredienten_koppel INNER JOIN ingredienten ON ingredienten.id_ingredient = ingredienten_koppel.id_ingredient WHERE ingredienten_koppel.id=$kookbook_row[0]")
or die(mysql_error());
while ($ingredient_row = mysql_fetch_row($ingredient_result))
{
$hoeveelheid = $ingredient_row[4];
$ingredient = $ingredient_row[1];
print("$hoeveelheid $ingredient<br>");
}
}
print("<b>bereidingswijze:</b><br>");
print("$bereidingswijze<br>");
print("<b>opmerking:</b><br>");
print("$opmerkingen<br>");
print("$foto<br>");
}
{/php}
|
But i want to learn how to get the php code out of my template so please help me learn smarty
Last edited by misterd on Mon Mar 26, 2007 11:22 am; edited 1 time in total |
|
Back to top |
|
Hielke Hoeve Smarty Elite
Joined: 06 Jan 2006 Posts: 406 Location: Netherlands
|
Posted: Mon Mar 26, 2007 8:35 am Post subject: |
|
|
Isn't even set, so why do u expect to get a value out of that? I think you'll have to use $home as this is the variable where something is inserted into by the 1st query. _________________ Debug XHTML Compliance
SmartyPaginate
Smarty License Questions
---
(About Unix) The learning curve is full of aha! moments, such as that glorious day that the full beauty of grep and, later, find is revealed in all its majesty. --- Robert Uhl <ruhl@4dv.net> |
|
Back to top |
|
misterd Smarty n00b
Joined: 24 Mar 2007 Posts: 2
|
Posted: Mon Mar 26, 2007 11:09 am Post subject: |
|
|
I am a bit further now.
i know now that my smarty tryout is rubbish...
so the question is changed to how to rewrite this code into smarty code
Code: |
{php}
$kookboek_result = mysql_query("SELECT kookboek.*, categorie.*, land.* from kookboek INNER JOIN categorie ON kookboek.categorie_id = categorie.categorie_id INNER JOIN land ON kookboek.land_id = land.land_id")
or die(mysql_error());
while ($kookbook_row = mysql_fetch_row($kookboek_result))
{
$gerechtnaam = $kookbook_row[1];
$categorie = $kookbook_row[8];
$land = $kookbook_row[10];
$bereidingswijze = $kookbook_row[4];
$opmerkingen = $kookbook_row[5];
$foto = $kookbook_row[6];
print("<b>gerechtnaam:</b> $gerechtnaam<br>");
print("<b>categorie:</b> $categorie<br>");
print("<b>land:</b> $land<br>");
print("<b>ingredienten:</b><br>");
{
$ingredient_result =
mysql_query("SELECT ingredienten.*, ingredienten_koppel.* from ingredienten_koppel INNER JOIN ingredienten ON ingredienten.id_ingredient = ingredienten_koppel.id_ingredient WHERE ingredienten_koppel.id=$kookbook_row[0]")
or die(mysql_error());
while ($ingredient_row = mysql_fetch_row($ingredient_result))
{
$hoeveelheid = $ingredient_row[4];
$ingredient = $ingredient_row[1];
print("$hoeveelheid $ingredient<br>");
}
}
print("<b>bereidingswijze:</b><br>");
print("$bereidingswijze<br>");
print("<b>opmerking:</b><br>");
print("$opmerkingen<br>");
print("$foto<br>");
}
{/php}
|
this part of the above code:
Code: |
{
$ingredient_result =
mysql_query("SELECT ingredienten.*, ingredienten_koppel.* from ingredienten_koppel INNER JOIN ingredienten ON ingredienten.id_ingredient = ingredienten_koppel.id_ingredient WHERE ingredienten_koppel.id=$kookbook_row[0]")
or die(mysql_error());
while ($ingredient_row = mysql_fetch_row($ingredient_result))
{
$hoeveelheid = $ingredient_row[4];
$ingredient = $ingredient_row[1];
print("$hoeveelheid $ingredient<br>");
}
}
|
is now changed to
Code: |
$conn = mysql_connect($hostname, $dbUser, $dbPass) or die("Cannot connect to the database");
mysql_select_db($dbName);
$sql = "SELECT ingredienten.id_ingredient AS id_ingredient, ingredienten.ingredient AS ingredient, ingredienten_koppel.id AS id, ingredienten_koppel.id_ingredient, ingredienten_koppel.hoeveelheid AS hoeveelheid
FROM ingredienten
INNER JOIN ingredienten_koppel ON ( ingredienten.id_ingredient = ingredienten_koppel.id_ingredient )";
// get all the products from the table
$res = mysql_query($sql);
$results = array();
$i=0;
while ($r=mysql_fetch_array($res)) {
$tmp = array(
'ingredient'=> $r['ingredient'],
'hoeveelheid'=> $r['hoeveelheid']
);
$results[$i++] = $tmp;
}
// pass the results to the template
$smarty->assign('results', $results);
|
|
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|