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

Self creating form with an array of an array

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


Joined: 13 Nov 2014
Posts: 1

PostPosted: Thu Nov 13, 2014 1:11 am    Post subject: Self creating form with an array of an array Reply with quote

I need to create a form that creates itself from a database with smarty. I believe I have the php page down, but I am having trouble displaying it.
Currently I am trying to use nested foreach loops to build the form:

Code:
{foreach $characteristics as $cur => $char}
            <h2>{$cur['Char_Name']}</h2>
            {foreach $cur['Option_Name'] as $Op_ID => $option}
               <label>
                  <input type="radio" value="{$Op_ID}" ..>
                     {$option}
               </label>
            {/foreach}
         {/foreach}


The way it should look is that there should be a title (ex. car) from the outer loop and should have radio button options underneath that (ex. -Ford -Honda -Chevy)

My sql statement and array, along with the variables I have assigned:
Code:

$query= "SELECT Characteristic.Name AS Char_Name, C_Option.Name AS Option_Name, Characteristic.Characteristic_ID AS Char_ID, C_Option.Option_ID AS Op_ID
      FROM Characteristic
      JOIN C_Option ON Opt_Characteristic_ID = Characteristic_ID
      WHERE char_shape_category_ID = :shape";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":shape", $_GET["chosenShape"]);
$stmt->execute();

$current="";
$firstTime = True;
$cur = 0;
$characteristics = array();

While ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
   if($cur != $row['Char_ID']){
      if(!$firstTime){
         $characteristics[$cur] = array (
            "Name" => $name,
            "Option" => $optionarr
         );
      } else {
         $firstTime = False;
      }
      $name = $row['Char_Name'];
      $cur = $row['Char_ID'];
      $optionarr = array();
      
   }
   $optionarr[$row['Op_ID']] = $row['Option_Name'];
   
}

$$smarty -> assign("characteristics", $characteristics);


sorry if too specific
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Nov 21, 2014 12:34 am    Post subject: Reply with quote

It's simple. Enable debugging.
Also, you're doing too much work in your code.
Learn proper PDO initialization.
I.e.
Code:
try
{
  // PDO driver connection
  $_pdo = new PDO($DSN, 'user', 'pass',
    array( // Driver-specific options
      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
      PDO::ATTR_PERSISTENT => false,
      PDO::ATTR_EMULATE_PREPARES => 0,
      PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
      // MySQL exclusive stuff
      PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8;',
    ));
}
catch(Exception $e)
{
  error_log('Unable to connect to database. (' . $e->getMessage() . ')');
  die(1);
}
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