Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Simplist Thing Does Not Work.

This is a discussion on Simplist Thing Does Not Work., within the PHP section. This forum and the thread "Simplist Thing Does Not Work." are both part of the Programming Your Website category.

 
Reply to this topicStart new topic
> Simplist Thing Does Not Work.
Jason
post Mar 26 2008, 11:51 AM
Post #1


Master of the Universe
******

Group: Advisors
Posts: 1,157
Joined: 15-February 08
From: London, England
Member No.: 141



BEFORE READING LOOK AT SCREENSHOTS, NOTICE MY DATABASE COST COLUMN THEN MY SITE COST COLUMN. If someone can help me spot this bug I will be eternally grateful!

The table is populated. The values are not showing.

This is my html.

CODE
<?php
                for ($i=0; $i<count($id); $i++)
                {
                ?>
                    <tr>
                        <td>
                            <form method="post">
                                <input type="hidden" name="id" value="<?php print $id[$i]; ?>" />
                                
                                <?php
                                    if ($parent[$i] != '')
                                    {
                                        print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                                    }
                                ?>
                                <input type="text" name="name" value="<?php print $name[$i]; ?>" />
                        </td>
                        <td>
                            <select name="parent">
                                <option value="">Select parent</option>                                
                                <?php
                                if (isset($categories) && count($categories) > 0)
                                {
                                    for ($ii = 0; $ii < count($categories); $ii++)
                                    {
                                    ?>
                                        <option <?php if ($parent[$i] == $categories[$ii]) { print 'selected="selected"'; } ?> value="<?php print $categories[$ii]; ?>"><?php print $categories[$ii]; ?></option>
                                    <?php
                                    }
                                }
                                ?>
                            </select>
                        </td>
                        <td>                        
                            $<input type="text" name="cost" value="<?php print $cost[$i]; ?>" style="width:50px;" />
                        </td>
                        <td>
                            <input type="submit" name="edit" value="Edit" />
                        </td>
                        <td>
                            <input type="submit" name="delete" value="Delete" />
                            </form>
                        </td>
                    </tr>                    
                <?php
                }
                ?>


Now, the cost field is the problem. Everything else is displaying properly.

This is my sql.
CODE
/*Load Catergories */
        $this->db->from('categories');
        $this->db->where('parent', '');
        $query = $this->db->get();
        foreach ($query->result() as $row)
        {
            $data['id'][] = $row->id;
            $data['name'][] = $row->name;
            $data['parent'][] = $row->parent;
            $data['cost'][] = $row->cost;
            
            $this->db->from('categories');
            $this->db->where('parent', $row->name);
            $queries = $this->db->get();
            foreach ($queries->result() as $rows)
            {
                $data['id'][] = $rows->id;
                $data['name'][] = $rows->name;
                $data['cost'][] = $row->cost;                
                $data['parent'][] = $rows->parent;
            }
        }


I'm using code igniter, if you are not familiar with it i am sure you can work out what the ultra simplistic queries are doing.

Attached are pictures of my database and what my site is showing.

For some reason the cost column is showing up as all zeros. This is dispite the fact that several db columns have number values in them. None of the other columns are effected by this.

I am hoping that a fresh pair of eyes can solve this as I have been staring at it for 20 minutes using a phrases such as 'wtf!?' 'omg!', 'omgwtf!?' etc etc


Full code.

function
CODE
function CategoryManager($id = NULL)
    {
    
        /* Define Data Array */
        $data = array();
        $data['css'] = $this->css;
        $data['base'] = $this->base;
        
        /* Load Helpers */
        $this->load->database();
        $this->load->helper('form');
        $this->load->helper('file');
        $this->load->library('session');
        
        /* Add Category */
        if (isset($_POST['add']))
        {
            $name = $_POST['name'];
            $parent = $_POST['parent'];
            $cost = $_POST['cost'];
            $db = array (
                'name' => $name,
                'parent' => $parent,
                'cost' => $cost
            );
            $this->db->insert('categories', $db);
        }
        /* Edit Category */
        if (isset($_POST['edit']))
        {
            $name = $_POST['name'];
            $parent = $_POST['parent'];
            $cost = $_POST['cost'];
            $id = $_POST['id'];
            $this->db->set('parent', $parent);
            $this->db->set('cost', $cost);
            $this->db->set('name', $name);
            $this->db->where('id', $id);
            $this->db->update('categories');
        }
        
        /* Delete Category */
        if (isset($_POST['delete']))
        {
            $id = $_POST['id'];
            $this->db->where('id', $id);
            $this->db->delete('categories');
        }
        
        /*Load Catergories */
        $this->db->from('categories');
        $this->db->where('parent', '');
        $query = $this->db->get();
        foreach ($query->result() as $row)
        {
            $data['id'][] = $row->id;
            $data['name'][] = $row->name;
            $data['parent'][] = $row->parent;
            $data['cost'][] = $row->cost;
            
            $this->db->from('categories');
            $this->db->where('parent', $row->name);
            $queries = $this->db->get();
            foreach ($queries->result() as $rows)
            {
                $data['id'][] = $rows->id;
                $data['name'][] = $rows->name;
                $data['cost'][] = $row->cost;                
                $data['parent'][] = $rows->parent;
            }
        }
        
        $this->db->from('categories');
        $this->db->where('parent', '');
        $query = $this->db->get();
        foreach ($query->result() as $row) {
            $data['categories'][] = $row->name;
        }
        
        $this->load->view('admin_categories', $data);        
    }


HTML

CODE
<?php
    $this->load->view('admin_header.php');
?>

<div class="main">
    <div id="full">
        <div class="content">
            <h1>List members</h1>
            <p>The list below displays the members registered to the website</p>
            <table cellspacing="0">
                <tr>
                    <th>Category Name</th>
                    <th>Parent</th>
                    <th>Charge</th>
                    <th>Edit</th>
                    <th>Delete</th>
                </tr>
                
                <?php
                for ($i=0; $i<count($id); $i++)
                {
                ?>
                    <tr>
                        <td>
                            <form method="post">
                                <input type="hidden" name="id" value="<?php print $id[$i]; ?>" />
                                
                                <?php
                                    if ($parent[$i] != '')
                                    {
                                        print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                                    }
                                ?>
                                <input type="text" name="name" value="<?php print $name[$i]; ?>" />
                        </td>
                        <td>
                            <select name="parent">
                                <option value="">Select parent</option>                                
                                <?php
                                if (isset($categories) && count($categories) > 0)
                                {
                                    for ($ii = 0; $ii < count($categories); $ii++)
                                    {
                                    ?>
                                        <option <?php if ($parent[$i] == $categories[$ii]) { print 'selected="selected"'; } ?> value="<?php print $categories[$ii]; ?>"><?php print $categories[$ii]; ?></option>
                                    <?php
                                    }
                                }
                                ?>
                            </select>
                        </td>
                        <td>                        
                            $<input type="text" name="cost" value="<?php print $cost[$i]; ?>" style="width:50px;" />
                        </td>
                        <td>
                            <input type="submit" name="edit" value="Edit" />
                        </td>
                        <td>
                            <input type="submit" name="delete" value="Delete" />
                            </form>
                        </td>
                    </tr>                    
                <?php
                }
                ?>
                
                <tr>
                    <td colspan="5">
                        <form method="post">
                            <input type="text" name="name" />
                            <select name="parent">
                                <option value="">Select parent</option>
                                <?php
                                if (isset($categories) && count($categories) > 0)
                                {
                                    for ($i = 0; $i < count($categories); $i++)
                                    {
                                    ?>
                                        <option value="<?php print $categories[$i]; ?>"><?php print $categories[$i]; ?></option>
                                    <?php
                                    }
                                }
                                ?>
                            </select>
                            $<input type="text" name="cost" value="<?php print $cost[$i]; ?>" style="width:50px;" />
                            <input type="submit" name="add" value="Add" class="submit" />
                        </form>
                    </td>
                </tr>
            </table>                    
        </div>
    </div>
</div>    

<div id="footer">
    <span>Site Design and coding ©2007 :: Site Designed by Jason Stanley.</span>
</div>

Attached File(s)
Attached File  db.png ( 134.09K ) Number of downloads: 7
Attached File  site.png ( 111.62K ) Number of downloads: 7
 


--------------------
Go to the top of the page
 
+Quote Post
christopher
post Mar 26 2008, 08:22 PM
Post #2


Fresh Squeezed
**

Group: Members
Posts: 27
Joined: 15-February 08
From: Ottawa, Canada
Member No.: 154



My guess: the line below in bold should be $rows-> (with an 's').

CODE
/*Load Catergories */
$this->db->from('categories');
$this->db->where('parent', '');
$query = $this->db->get();
foreach ($query->result() as $row)
{
$data['id'][] = $row->id;
$data['name'][] = $row->name;
$data['parent'][] = $row->parent;
$data['cost'][] = $row->cost;

$this->db->from('categories');
$this->db->where('parent', $row->name);
$queries = $this->db->get();
foreach ($queries->result() as $rows)
{
$data['id'][] = $rows->id;
$data['name'][] = $rows->name;
$data['cost'][] = $row->cost;
$data['parent'][] = $rows->parent;
}
}
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   12 gribble 427 19th February 2008 - 05:49 AM
Last post by: welshstew
No New Posts   11 Jason 336 19th March 2008 - 06:57 PM
Last post by: Monie
No New Posts   1 Jason 198 21st May 2008 - 11:49 AM
Last post by: paintingtheweb
No New Posts   1 LivingDeadBeat 128 27th March 2008 - 03:16 PM
Last post by: Rakuli
No New Posts   4 Jason 119 7th April 2008 - 03:25 AM
Last post by: c010depunkk