Populating An Array From Sql

Discussion in 'PHP' started by trenttdogg, Feb 14, 2013.

  1. #1
    I am stuck. Please help. I have a function on page that gets information from the arrays (on the page). It works fine as far as getting the info from the array and displaying it as it should on the page. What I want is to have the arrays generated automatically from the data in mysql and placed 20 per page. Also do I need to set up separate tables for each "category" (ex: math, reading, writing) or is it possible to "fetch" data that has, for example, reading in the category column?
    My code is below.
    <?php
       
        function itemContainer($item) {
            echo '
            <div class="itemContainer">
                <div class="left">   
                    <a href="#v" class="plate">
                    <img src="',$item['src'],'" alt="',$item['alt'],'" />
                      </a>
                <!--.left --></div>
                <div class="middle">
                    <h1>',$item['title'],'</h1>
                    <p>',$item['text'],'</p>
                    <div>',$item['grades'],'</div>
                <!-- .middle --></div>
                <div class="right">
                    ',$item['price'],'
                <!-- .right --></div>
            <!-- .itemContainer --></div>';
        }
       
        $itemList = array(
    array(
                'src'        => 'some text',
                'alt'        => 'more text',
                'title'      => 'some text',
                'text'      => 'some description',
                'grades'    => 'First Second',
                'price'      => '<del>$5.00</del><span></span>'
            ),
        );
       
        foreach ($itemList as $item) itemContainer($item);
       
        ?>
    Code (markup):
    Thanks
     
    trenttdogg, Feb 14, 2013 IP
  2. Aman Arora

    Aman Arora Active Member

    Messages:
    100
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    78
    #2
    I assume following things
    • You have a table that contains all the data that you need
    • Table consists of columns src, alt, title, text, grades, price
    • You know how to connect to database using PHP
    Now to fetch a row from database and store it into an array. The PHP function that you will need is mysql_fetch_array($r,MYSQL_ASSOC). It will take 2 arguments, first is the resource variable which is returned by mysql_connect when you connect to the database. Second one is a constant value i.e "MYSQL_ASSOC" this will return results array with associative index i.e. name of index will be same as column names.
    About the other query related to category, You should create another table for category, in which all the categories with reside. Make two columns in this table i.e ID and category_name. Use this id as foreign key in the other table. You can then filter the results using sql where clause to fetch data in certain specific category
     
    Aman Arora, Feb 15, 2013 IP
    wtg likes this.