Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

getting category and it's subcategory from mysql with PDO

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
chister
Smarty n00b


Joined: 27 Apr 2011
Posts: 3

PostPosted: Sat Aug 06, 2011 8:09 pm    Post subject: getting category and it's subcategory from mysql with PDO Reply with quote

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 Smile
Back to top
View user's profile Send private message
imars
Smarty n00b


Joined: 20 Sep 2011
Posts: 1

PostPosted: Tue Sep 20, 2011 7:30 am    Post subject: version PDO Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Sep 20, 2011 1:56 pm    Post subject: Reply with quote

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}
    &nbsp;{$subcat.name}-{$subcat.child}<br>
  {/foreach}
{/foreach}
Back to top
View user's profile Send private message Visit poster's website
cilipe
Smarty n00b


Joined: 10 Nov 2015
Posts: 3

PostPosted: Tue Nov 10, 2015 7:21 pm    Post subject: Reply with quote

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}
&nbsp;-&nbsp;{$inc.name}<br />
{/if}
{/foreach}
{/foreach}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Nov 10, 2015 11:14 pm    Post subject: Reply with quote

Four years and wrong solution.
Back to top
View user's profile Send private message
cilipe
Smarty n00b


Joined: 10 Nov 2015
Posts: 3

PostPosted: Wed Nov 11, 2015 4:13 am    Post subject: Reply with quote

which part is wrong?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Nov 11, 2015 4:42 am    Post subject: Reply with quote

Everything, from top to bottom. Basically every other line is wrong.
Back to top
View user's profile Send private message
cilipe
Smarty n00b


Joined: 10 Nov 2015
Posts: 3

PostPosted: Wed Nov 11, 2015 6:17 am    Post subject: Reply with quote

@AnrDaemon: Say No To Drugs
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP