View previous topic :: View next topic |
Author |
Message |
chister Smarty n00b
Joined: 27 Apr 2011 Posts: 3
|
Posted: Sat Aug 06, 2011 8:09 pm Post subject: getting category and it's subcategory from mysql with PDO |
|
|
Hello,
I have table which stores categories and it's subcategories and I want to get them with smarty using PDO..
I tried everything with no hope..
Here's my non-smarty php code:
Code: | $sql = mysql_query("SELECT * FROM categories WHERE child = '0'");
while($row = mysql_fetch_array($sql)) {
echo $row['name']. " - " .$row['child'];
echo "<br>";
$sqll = mysql_query("SELECT * FROM categories WHERE child = '" .$row['id']. "'");
while($roww = mysql_fetch_array($sqll)) {
echo $roww['name']. " - " .$roww['child'];
echo "<br>";
}
}
|
Wish someone can help me |
|
Back to top |
|
imars Smarty n00b
Joined: 20 Sep 2011 Posts: 1
|
Posted: Tue Sep 20, 2011 7:30 am Post subject: version PDO |
|
|
Code: |
$sql1 = $bdd->prepare("SELECT * FROM categories WHERE child = '0' ");
$req1->execute();
while($row = $req1->fetch(PDO::FETCH_OBJ)){
echo $row['name']. " - " .$row['child'];
$sql2 = $bdd->prepare("SELECT * FROM categories WHERE child = '" .$row['id']. "'");
$req2->execute();
while($row2 = $req2->fetch(PDO::FETCH_OBJ)){
echo " &nbps; ". $row2['name']. " - " .$row2['child']."<br>";
}
}
|
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Sep 20, 2011 1:56 pm Post subject: |
|
|
Instead of echo, assign the data to nested arrays, something like:
Code: | $sql1 = $bdd->prepare("SELECT * FROM categories WHERE child = '0' ");
$req1->execute();
$cats = array(); // init array
while($row = $req1->fetch(PDO::FETCH_OBJ)){
// do not echo
//echo $row['name']. " - " .$row['child'];
$sql2 = $bdd->prepare("SELECT * FROM categories WHERE child = '" .$row['id']. "'");
$req2->execute();
$subcats = array(); // init array
while($row2 = $req2->fetch(PDO::FETCH_OBJ)){
// do not echo
//echo " &nbps; ". $row2['name']. " - " .$row2['child']."<br>";
// assign subcats to an array
$subcats[] = $row2;
}
// assign subcats to current row
$row['subcats'] = $subcats;
// assign rows to array
$cats[] = $row;
}
|
Now assign that data to Smarty:
Code: | $smarty->assign('cats',$cats);
$smarty->display('mypage.tpl'); |
Then in the template, you can now loop over that data and display it.
mypage.tpl:
Code: | {foreach $cats as $cat}
{$cat.name}-{$cat.child}
{foreach $cat.subcats as $subcat}
{$subcat.name}-{$subcat.child}<br>
{/foreach}
{/foreach} |
|
|
Back to top |
|
cilipe Smarty n00b
Joined: 10 Nov 2015 Posts: 3
|
Posted: Tue Nov 10, 2015 7:21 pm Post subject: |
|
|
PHP
Code: | $recordSet = $conn->Execute('SELECT * FROM category ORDER BY name ASC');
if (!$recordSet)
print $conn->ErrorMsg();
else
while (!$recordSet->EOF) {
if ($recordSet->fields['level'] == 0) {
$aval[] = $recordSet->fields;
} else {
$nval[] = $recordSet->fields;
}
$recordSet->MoveNext();
}
$smarty->assign('category',@$aval);
$smarty->assign('subcat',@$nval); |
HTML
Code: | {foreach from=$category item=caty}
{$caty.name}<br />
{foreach from=$subcat item=inc}
{if $inc.level neq 0 && $caty.catid eq $inc.parent}
- {$inc.name}<br />
{/if}
{/foreach}
{/foreach}
|
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Nov 10, 2015 11:14 pm Post subject: |
|
|
Four years and wrong solution. |
|
Back to top |
|
cilipe Smarty n00b
Joined: 10 Nov 2015 Posts: 3
|
Posted: Wed Nov 11, 2015 4:13 am Post subject: |
|
|
which part is wrong? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Nov 11, 2015 4:42 am Post subject: |
|
|
Everything, from top to bottom. Basically every other line is wrong. |
|
Back to top |
|
cilipe Smarty n00b
Joined: 10 Nov 2015 Posts: 3
|
Posted: Wed Nov 11, 2015 6:17 am Post subject: |
|
|
@AnrDaemon: Say No To Drugs |
|
Back to top |
|
|