Jump to content

Recommended Posts

Hi, I want to put a column from mysql into an array.  Should I use fetch() or fetchAll() ?

 

            try
            {  
                $sth = $dbh->prepare("SELECT name FROM institution");
                $sth->execute();


                /* Fetch all of the institution names from table*/
                $institutions = $sth->fetchAll();
            }
            catch(PDOException $e) {
                    die($e->getMessage());
            }

I  then want to iterate through the array and populate a dropdown using the array.  

 

        foreach ($arrValues as $row)
        {
           $id = $row['employee_id'];
           echo '<tr>
                        <td><input type="text" name="record['.$id.'][firstName]" value="'.$row['first_name'].'" . $readState . /></ td>
                        <td><input type="text" name="record['.$id.'][lastName]" value="'.$row['last_name'].'" . $readState . /></ td>
                        <td><input type="text" name="record['.$id.'][height]" value="'.$row['height'].'" . $readState . /></ td>
                        <td><input type="text" name="record['.$id.'][cap]" value="'.$row['cap'].'" . $readState . /></ td>
                        <td><input type="text" name="record['.$id.'][color]" value="'.$row['colors'].'" . $readState . /></ td>


                                <td><select name="record['.$id.'][degree]"> </td>
                                <option> "Filler" </option>
                                </select> 




                                <td><select name="record['.$id.'][school]"> </td> '
                                   
                                    if($edit){
                                        foreach($institutions as $option){


                                           echo "<option value='{$option}' </option>";   


                                        }
                                    }
                                    #edit =!$edit;
                               echo '</select> 


                        <td><input type="submit" name="update" value="Update" /></td>
                    </tr>';
If the edit variable == true I want to fill the dropdowns.  The error says, "Unexpected if"
 
Can anyone explain this?
Edited by shaung
Link to comment
https://forums.phpfreaks.com/topic/284965-putting-a-db-column-into-an-array/
Share on other sites

OK, thanks.  I still can't get the values from the database into the array then into the dropdown. 

 

Someone suggested I do this:

 


function _options($arr){
    $selected = isset($arr['selected']) ? " selected='selected'" : '';
    return "<option value='{$arr['id']}'$selected>{$arr['name']}</option>";
}
$current_options = $institutions;
$options = array_map('_options',$current_options);


echo implode("\n",$options);

...but I can't get that to work either.  

 

 I have like one day a week that I can look at this and I have been stuck on it for three weeks.  This sucks to no end.

 

Here is all the pertinent code using the above method.

 

 
        $_SESSION['sid'] = $sid;        
        $edit = false;
        /*-------------------------------------
        *Begin adding your stuff here 
        --------------------------------------*/
            /*-------------------------------------  */   
            
            if($_SESSION['edit'])
            {
               $sql = "SELECT * FROM fac_detail INNER JOIN institution ON fac_detail.institution_id = institution.id "
                . "INNER JOIN degree ON fac_detail.degree_id = degree.id WHERE employee_id = . '$_POST[hidden]'";
            } 
            else 
            {


                $sql = "SELECT * FROM fac_detail INNER JOIN institution ON fac_detail.institution_id = institution.id "
                . "INNER JOIN degree ON fac_detail.degree_id = degree.id"; 
            }
            
            try
            {  
                $sth = $dbh->prepare("SELECT name FROM institution");
                $sth->execute();


                /* Fetch all of the values of the first column */
                $institutions = $sth->fetch();
            }
            catch(PDOException $e) {
                    die($e->getMessage());
            }
            
            //try
            //{  
                //$sth = $dbh->prepare("SELECT degree_type FROM degree");
                //$sth->execute();


                /* Fetch all of the values of the column */
                //$degrees = $sth->fetch();
            //}
            //catch(PDOException $e) {
                    //die($e->getMessage());
            //}


            try
            {  
                    if(isset($_POST['update']))
                    {
                        $updatedQuery = "UPDATE fac_detail SET first_name=:fname, last_name=:lname, height=:height, cap=:cap,colors=:color WHERE employee_id=:id";
                        $stmt = $dbh->prepare($updatedQuery);


                        // loop over each record in $_POST['record'] 
                        // getting the employee_id and each fields value
                        foreach($_POST['record'] as $employee_id => $field)
                        {
                            $stmt->bindParam(':fname', $field['firstName'], PDO::PARAM_STR);
                            $stmt->bindParam(':lname', $field['lastName'], PDO::PARAM_STR);
                            $stmt->bindParam(':height', $field['height'], PDO::PARAM_STR);
                            $stmt->bindParam(':cap', $field['cap'], PDO::PARAM_STR);
                            $stmt->bindParam(':color', $field['color'], PDO::PARAM_STR);
                            $stmt->bindParam(':id', $employee_id, PDO::PARAM_INT);


                            $stmt->execute();
                        }
                    }


                //$sql = "SELECT * FROM fac_detail";
                $stmt = $dbh->prepare($sql);
                $stmt->execute();
                $arrValues = $stmt->fetchAll(PDO::FETCH_ASSOC);
                $row = $stmt->fetch();
            }
            catch(PDOException $e) {
                    die($e->getMessage());
            }


    //Heredoc syntax for echoing out HTML
    echo <<<HTML
    <form method= post>
        <table id="recordTable">
            <thead>
                <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Height</th>
                    <th>Cap</th>
                    <th>Color</th>
                    <th>Degree</th>
                    <th>Institution</th>
                    <th>Edit Record</th>
                </tr>
            </thead>
            
HTML;


        //Array with sid as key
    
    if($edit)
    {
        $readstate = "readonly";
    }
    else 
    {
        $readstate = "";
    }
               
            echo "<tbody>";
     
        foreach ($arrValues as $row)
        {
           $id = $row['employee_id'];
           echo '<tr>
                        <td><input type="text" name="record['.$id.'][firstName]" value="'.$row['first_name'].'" . $readState . /></ td>
                        <td><input type="text" name="record['.$id.'][lastName]" value="'.$row['last_name'].'" . $readState . /></ td>
                        <td><input type="text" name="record['.$id.'][height]" value="'.$row['height'].'" . $readState . /></ td>
                        <td><input type="text" name="record['.$id.'][cap]" value="'.$row['cap'].'" . $readState . /></ td>
                        <td><input type="text" name="record['.$id.'][color]" value="'.$row['colors'].'" . $readState . /></ td>


                                <td><select name="record['.$id.'][degree]"> </td>
                                <option> "Filler" </option>
                                </select> 




                                <td><select name="record['.$id.'][school]"> </td>';
                     
                                    if($edit){
                                        foreach($institutions as $val){
                                          echo "<option value='\$val\'>".$val."</option>";
                                        }
                                    }
                                    
                               echo '</select> 


                        <td><input type="submit" name="update" value="Update" /></td>
                    </tr>';
        }   
  


            echo "</tbody>";
        echo "</table>";
       echo "</form>";
       
       
?>
 
Everything above and below the code is done with a template and works fine.  Everything works on this page except the dropdowns.  Any hints as to how I can get this to work would be greatly appreciated.
 
When the page loads it shows a table full of form elements filled with data and an edit button for each row.  I want them to click the edit button then show the table again with only the single record they clicked with the dropdowns filled.  
Edited by shaung

 

You the syntax for the dropdown options is wrong

echo "<option value='{$option}' </option>"; 

the above should be like

echo "<option value='{$option}'>$option</option>"; 

//OR Just use
echo "<option>$option</option>"; 

For some reason nothing I do will fill the options

 

                                <td><select name="record['.$id.'][school]"> </td>';
                                    if($edit){
                                        foreach($institutions as $option){
                                          echo "<option>$option</option>"; 
                                        }
                                    }
                                    
                               echo '</select> 

Thanks for taking your time with this.  I will look at this again tonight after work.

Edited by shaung
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.