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

Mysql array pulling certain fields and values

 
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
tornado9731
Smarty n00b


Joined: 10 May 2017
Posts: 1

PostPosted: Thu May 11, 2017 1:13 am    Post subject: Mysql array pulling certain fields and values Reply with quote

Hello, I am new to smarty. I am trying to figure out how to pull data from array from a mysql php file. I do not want all the fields in the table just three. I am trying to only pull id name and category fields and values and loop through them in a table. I don't need to display all the fields. i know I can select the fields in my select statement, but I want to understand how to pull certain fields and values to display them only if my select has *.

Code:

 $query = "SELECT * FROM Recipe;";
 $result = mysqli_query($db_server,$query);
     
      while($row = mysqli_fetch_assoc($result)){     
                    $id=$row["id"];
                    $name = $row["name"];
                    $description =$row["description"];
                    $instructions = $row["instructions"];
                    $category =$row["category"];
                    $created_date = $row["created_date"];
                    $rows[] = $row; # $data is the array created for use in the Smarty template.

//Assign PHP variable to Smarty template
$tpl->assign('rows', $rows);
$tpl->assign('id', $id);
$tpl->assign('name', $name);
$tpl->assign('description', $description);
$tpl->assign('instructions', $instructions);
$tpl->assign('category', $catagory);               
$tpl->assign('created_date', $created_date);
            }

// Display template
    $tpl->display('listrecipes.tpl');
?>


My smarty template listrecipes.tpl
Code:

 <!-- page title -->
         <h1>List of All Recipes</h1>
         <table border="1" cellspacing="1" cellpadding="1">
            <tbody>
               {if $rows}
                 <tr>
                  <th scope="col">Recipes</th>
               </tr>
              {section name=row loop=$rows}
                  <tr>
{foreach from=$rows[row] key="Key" item="Value"}
                      <td><a href="recipe_detail.php?={$Value}">{$Value}</a></td>
                  {/foreach}     
               </tr>
               {/section}
               {else}
               <h1>NO ROWS FOUND</h1>
               {/if}
            </tbody>
         </table>
      </div>
   </body>
</html


I would appreciate any help with this and the proper code. I have it displaying all the fields.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu May 11, 2017 9:30 am    Post subject: Re: Mysql array pulling certain fields and values Reply with quote

tornado9731 wrote:
Hello, I am new to smarty. I am trying to figure out how to pull data from array from a mysql php file.
How is this a Smarty problem?
Quote:
I do not want all the fields in the table just three.
Then request only these fields, instead of all of them.
Code:
<?php

$pdo = new PDO('mysql:...', $user, $pass, [
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  PDO::ATTR_EMULATE_PREPARES => false,
  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);

$query = $pdo->prepare("SELECT `id`, `name`, `description`, `instructions`, `category`, UNIX_TIMESTAMP(`created_date`) `date`
  FROM Recipe;");
$query->execute();
$tpl->assign('rows', $query->fetchAll());

// Display template
$tpl->display('listrecipes.tpl');
?>


Code:

<!-- page title -->
<h1>Recipes</h1>
{foreach $rows as $row}
{if $row@first}
<table border="1" cellspacing="1" cellpadding="1">
  <tbody>
    <tr>
      <th scope="col">Recipes</th>
    </tr>
{/if}
    <tr>
      <td><a href="recipe_detail.php?={$row.id}">{$row.name}</a> ({$date|date_format:"%F %H:%M"})</td>
    </tr>
{if $row@last}
  </tbody>
</table>
{foreachelse}
<h2>No recipes found</h2>
{/foreach}
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