SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to use this array like table in database?

    Hi, all! I want to take the first 5 records in array below, but I can't. Can you help me? I don't know name of this array, I'm only see it like table in database.

    PHP Code:
    <?php
        $products 
    = array(
            array(
    'id' => '1''name' => 'broccoli''desc' => 'broccoli''price' => '1200'),
            array(
    'id' => '2''name' => 'butter''desc' => 'butter''price' => '1300'),
            array(
    'id' => '3''name' => 'cheese''desc' => 'cheese''price' => '1400'),
            array(
    'id' => '4''name' => 'chickenleg''desc' => 'chicken leg''price' => '1500'),
            array(
    'id' => '5''name' => 'chocolatecookies''desc' => 'chocolate cookies''price' => '1600'),
            array(
    'id' => '6''name' => 'cornonthecob''desc' => 'corn on the cob''price' => '1700'),
            array(
    'id' => '7''name' => 'freerangeeggs''desc' => 'free range eggs''price' => '1800'),
            array(
    'id' => '8''name' => 'milk''desc' => 'milk''price' => '1900'),
            array(
    'id' => '9''name' => 'organicmeatpatties''desc' => 'organic meat patties''price' => '2000'),
            array(
    'id' => '10''name' => 'parmaham''desc' => 'parma ham''price' => '2010'),
            array(
    'id' => '11''name' => 'pumpkinseedbun''desc' => 'pumpkin seed bun''price' => '2020'),
            array(
    'id' => '12''name' => 'redcurrants''desc' => 'red currants''price' => '2030'),
            array(
    'id' => '13''name' => 'sausages''desc' => 'sausages''price' => '2040'),
            array(
    'id' => '14''name' => 'seedlesswatermelon''desc' => 'seedless watermelon''price' => '2050'),
            array(
    'id' => '15''name' => 'sesameseedbagel''desc' => 'sesame seed bagel''price' => '2060'),
            array(
    'id' => '16''name' => 'sunflowerseedloaf''desc' => 'sunflower seed loaf''price' => '2070')
        );
    ?>

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    78
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You can access each nested array using the automatically set numbered keys (with each array being the value). So in this case you could use a "for" loop to iterate through the first five sub-arrays:
    PHP Code:
    for($a=0;$a<=4;++$a)
    {
        
    print_r($products[$a]);

    #Outputs:
    Code:
    rray
    (
        [id] => 1
        [name] => broccoli
        [desc] => broccoli
        [price] => 1200
    )
    Array
    (
        [id] => 2
        [name] => butter
        [desc] => butter
        [price] => 1300
    )
    Array
    (
        [id] => 3
        [name] => cheese
        [desc] => cheese
        [price] => 1400
    )
    Array
    (
        [id] => 4
        [name] => chickenleg
        [desc] => chicken leg
        [price] => 1500
    )
    Array
    (
        [id] => 5
        [name] => chocolatecookies
        [desc] => chocolate cookies
        [price] => 1600
    )

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I'm only see it like table in database.
    Not sure I have read your request correctly, but it you only wanted the first 5 rows from the db in the first place, then you could have limited your db to only returning 5 with LIMIT.

    PHP Code:
    $sql "SELECT id
    , name
    , `desc`
    , price 
    FROM
    mytable 
    LIMIT 5"

    ** desc is a mysql protected word, hence it is quoted with backticks, presuming you ARE using mysql of course ... If you are not using mysql then your RDBMS will have its own way of limiting the number of returned rows.

  4. #4
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by modernW View Post
    You can access each nested array using the automatically set numbered keys (with each array being the value). So in this case you could use a "for" loop to iterate through the first five sub-arrays:
    PHP Code:
    for($a=0;$a<=4;++$a)
    {
        
    print_r($products[$a]);

    Quote Originally Posted by Cups View Post
    Not sure I have read your request correctly, but it you only wanted the first 5 rows from the db in the first place, then you could have limited your db to only returning 5 with LIMIT.

    PHP Code:
    $sql "SELECT id
    , name
    , `desc`
    , price 
    FROM
    mytable 
    LIMIT 5"

    ** desc is a mysql protected word, hence it is quoted with backticks, presuming you ARE using mysql of course ... If you are not using mysql then your RDBMS will have its own way of limiting the number of returned rows.
    Thank a lot! But I want to work with database is array, not mysql or somethings. I want display data in array like this:

    e3b78302fcaa714e480a0ecc7482e02b_48559899.2.png

    As you can see, I'm trying to paginate data. I calculated some cases and else display. I have a code but it not work:

    for($x=$start; $x<=5; $x++){
    echo '<tr>';
    echo '<td>'.$products['id'][$x].'</td>';
    echo '<td><img src=img/'.$products['name'][$x].'.png alt= />'.'</td>';
    echo '<td>'.$products['desc'][$x].'</td>';
    echo '<td>'.number_format($products['price'][$x], 2).'</td>';
    echo '</tr>';
    }
    Do you know what I said?
    Attached Images Attached Images

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Can you show us how $products is created/defined? We understand you want to treat it as an array (modernW's code did just that), however, since we don't know how you are defining $products, it is impossible for us to tell you if the issue lies in looping through the records or with how you created $products.

    My gut says, it is how your created $products, and that $products currently is NOT an array.

    I also want to point out that StarLion's suggestion is good too, and more likely you should be using LIMIT against your MySQL database (if that is what you are using), and using a second query to do the pagination by returning a COUNT of the products in the table.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  6. #6
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Can you show us how $products is created/defined? We understand you want to treat it as an array (modernW's code did just that), however, since we don't know how you are defining $products, it is impossible for us to tell you if the issue lies in looping through the records or with how you created $products.

    My gut says, it is how your created $products, and that $products currently is NOT an array.

    I also want to point out that StarLion's suggestion is good too, and more likely you should be using LIMIT against your MySQL database (if that is what you are using), and using a second query to do the pagination by returning a COUNT of the products in the table.
    Here is code index.php:

    PHP Code:
    <?php include('data.dat'); ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" type="text/css" href="css/paginate.css" />
        <title>Paginate</title>
    </head>
    <body>
        <?php
            
    //
            
    $start = (isset($_GET['start']) && (int)$_GET['start']) ? $_GET['start'] : 0;
            
    //
            
    $display 5;
            
    // 
            
    $records sizeof($products);
        
    ?>
        <table border="1">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Image</th>
                    <th>Name</th>
                    <th>Price</th>
                </tr>
            </thead>
            <tbody>
            <?php //Display data is here
                
    for($x=$start$x<=min($records$start+$display); $x++){
                    echo 
    '<tr>';
                    echo     
    '<td>'.$products['id'][$x].'</td>';
                    echo     
    '<td><img src=img/'.$products['name'][$x].'.png alt= />'.'</td>';
                    echo    
    '<td>'.$products['desc'].'</td>';
                    echo     
    '<td>'.number_format($products['price'][$x], 2).'</td>';
                    echo 
    '</tr>';
                }
            
    ?>
            </tbody>
            <tfoot>
                <tr>
                    <td>
                        
                        <?php
                            
    // 
                            
    if ($records $display){
                                
    // 
                                
    $pages ceil($records/$display);
                            } else {
                                
    $pages 1;
                            } 
                            
                            echo 
    '<td colspan=4>';
                                
    // 
                                
    if($start!=0){
                                    echo 
    '<a href=index.html.php?start='.($start-$display).'>Previous</a>';
                                }
                                
    // 
                                
    if($pages 1){
                                    echo 
    '<div id=page><ul>';
                                    for(
    $i=1$i<=$pages$i++){
                                        echo 
    '<li><a href=index.html.php?start='.$display*($i-1).'>'.$i.'</a></li>';
                                    }
                                    echo 
    '</ul></div>';
                                    
    // 
                                    
    if($start!= $display*($i-1)){
                                        echo 
    '<a href=index.html.php?start='.($start+$display).'>Next</a>';
                                    }
                                }
                            echo 
    '</td>';
                        
    ?>
                    </td>
                </tr>
            </tfoot>
        </table>
    </body>
    </html>
    And here is code data.dat:

    PHP Code:
    <?php
        $products 
    = array(
            array(
    'id' => '1''name' => 'broccoli''desc' => 'broccoli''price' => '1200'),
            array(
    'id' => '2''name' => 'butter''desc' => 'butter''price' => '1300'),
            array(
    'id' => '3''name' => 'cheese''desc' => 'cheese''price' => '1400'),
            array(
    'id' => '4''name' => 'chickenleg''desc' => 'chicken leg''price' => '1500'),
            array(
    'id' => '5''name' => 'chocolatecookies''desc' => 'chocolate cookies''price' => '1600'),
            array(
    'id' => '6''name' => 'cornonthecob''desc' => 'corn on the cob''price' => '1700'),
            array(
    'id' => '7''name' => 'freerangeeggs''desc' => 'free range eggs''price' => '1800'),
            array(
    'id' => '8''name' => 'milk''desc' => 'milk''price' => '1900'),
            array(
    'id' => '9''name' => 'organicmeatpatties''desc' => 'organic meat patties''price' => '2000'),
            array(
    'id' => '10''name' => 'parmaham''desc' => 'parma ham''price' => '2010'),
            array(
    'id' => '11''name' => 'pumpkinseedbun''desc' => 'pumpkin seed bun''price' => '2020'),
            array(
    'id' => '12''name' => 'redcurrants''desc' => 'red currants''price' => '2030'),
            array(
    'id' => '13''name' => 'sausages''desc' => 'sausages''price' => '2040'),
            array(
    'id' => '14''name' => 'seedlesswatermelon''desc' => 'seedless watermelon''price' => '2050'),
            array(
    'id' => '15''name' => 'sesameseedbagel''desc' => 'sesame seed bagel''price' => '2060'),
            array(
    'id' => '16''name' => 'sunflowerseedloaf''desc' => 'sunflower seed loaf''price' => '2070')
        );
    ?>
    Thank for watching!

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this using array_chunk would be best fit I think:
    Code php:
        $products = array( 
            array('id' => '1', 'name' => 'broccoli', 'desc' => 'broccoli', 'price' => '1200'), 
            array('id' => '2', 'name' => 'butter', 'desc' => 'butter', 'price' => '1300'), 
            array('id' => '3', 'name' => 'cheese', 'desc' => 'cheese', 'price' => '1400'), 
            array('id' => '4', 'name' => 'chickenleg', 'desc' => 'chicken leg', 'price' => '1500'), 
            array('id' => '5', 'name' => 'chocolatecookies', 'desc' => 'chocolate cookies', 'price' => '1600'), 
            array('id' => '6', 'name' => 'cornonthecob', 'desc' => 'corn on the cob', 'price' => '1700'), 
            array('id' => '7', 'name' => 'freerangeeggs', 'desc' => 'free range eggs', 'price' => '1800'), 
            array('id' => '8', 'name' => 'milk', 'desc' => 'milk', 'price' => '1900'), 
            array('id' => '9', 'name' => 'organicmeatpatties', 'desc' => 'organic meat patties', 'price' => '2000'), 
            array('id' => '10', 'name' => 'parmaham', 'desc' => 'parma ham', 'price' => '2010'), 
            array('id' => '11', 'name' => 'pumpkinseedbun', 'desc' => 'pumpkin seed bun', 'price' => '2020'), 
            array('id' => '12', 'name' => 'redcurrants', 'desc' => 'red currants', 'price' => '2030'), 
            array('id' => '13', 'name' => 'sausages', 'desc' => 'sausages', 'price' => '2040'), 
            array('id' => '14', 'name' => 'seedlesswatermelon', 'desc' => 'seedless watermelon', 'price' => '2050'), 
            array('id' => '15', 'name' => 'sesameseedbagel', 'desc' => 'sesame seed bagel', 'price' => '2060'), 
            array('id' => '16', 'name' => 'sunflowerseedloaf', 'desc' => 'sunflower seed loaf', 'price' => '2070') 
        );
        echo "<pre>"; print_r(array_chunk($products, 5, true));
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Somehow I missed this earlier, but your array dimensions are wrong
    PHP Code:
                    echo '<tr>';
                    echo     
    '<td>'.$products['id'][$x].'</td>';
                    echo     
    '<td><img src=img/'.$products['name'][$x].'.png alt= />'.'</td>';
                    echo    
    '<td>'.$products['desc'].'</td>';
                    echo     
    '<td>'.number_format($products['price'][$x], 2).'</td>';
                    echo 
    '</tr>'
    Should be
    PHP Code:
                    echo '<tr>';
                    echo     
    '<td>'.$products[$x]['id'].'</td>';
                    echo     
    '<td><img src=img/'.$products[$x]['name'].'.png alt= />'.'</td>';
                    echo    
    '<td>'.$products[$x]['desc'].'</td>';
                    echo     
    '<td>'.number_format($products[$x]['price'], 2).'</td>';
                    echo 
    '</tr>'
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  9. #9
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    $products = array( 
        array('id' => '1', 'name' => 'broccoli', 'desc' => 'broccoli', 'price' => '1200'), 
        array('id' => '2', 'name' => 'butter', 'desc' => 'butter', 'price' => '1300'), 
        array('id' => '3', 'name' => 'cheese', 'desc' => 'cheese', 'price' => '1400'), 
        array('id' => '4', 'name' => 'chickenleg', 'desc' => 'chicken leg', 'price' => '1500'), 
        array('id' => '5', 'name' => 'chocolatecookies', 'desc' => 'chocolate cookies', 'price' => '1600'), 
        array('id' => '6', 'name' => 'cornonthecob', 'desc' => 'corn on the cob', 'price' => '1700'), 
        array('id' => '7', 'name' => 'freerangeeggs', 'desc' => 'free range eggs', 'price' => '1800'), 
        array('id' => '8', 'name' => 'milk', 'desc' => 'milk', 'price' => '1900'), 
        array('id' => '9', 'name' => 'organicmeatpatties', 'desc' => 'organic meat patties', 'price' => '2000'), 
        array('id' => '10', 'name' => 'parmaham', 'desc' => 'parma ham', 'price' => '2010'), 
        array('id' => '11', 'name' => 'pumpkinseedbun', 'desc' => 'pumpkin seed bun', 'price' => '2020'), 
        array('id' => '12', 'name' => 'redcurrants', 'desc' => 'red currants', 'price' => '2030'), 
        array('id' => '13', 'name' => 'sausages', 'desc' => 'sausages', 'price' => '2040'), 
        array('id' => '14', 'name' => 'seedlesswatermelon', 'desc' => 'seedless watermelon', 'price' => '2050'), 
        array('id' => '15', 'name' => 'sesameseedbagel', 'desc' => 'sesame seed bagel', 'price' => '2060'), 
        array('id' => '16', 'name' => 'sunflowerseedloaf', 'desc' => 'sunflower seed loaf', 'price' => '2070') 
    );
    $chunks = array_chunk($products, 5, true);
    $page = isset($_GET['page']) ? $_GET['page'] : 0;
    if(isset($chunks[$page])){
        foreach($chunks[$page] as $product){
            echo $product['id'] . ' = ' . $product['name'] . '<br />';
        }
    }
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  10. #10
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Somehow I missed this earlier, but your array dimensions are wrong
    PHP Code:
                    echo '<tr>';
                    echo     
    '<td>'.$products['id'][$x].'</td>';
                    echo     
    '<td><img src=img/'.$products['name'][$x].'.png alt= />'.'</td>';
                    echo    
    '<td>'.$products['desc'].'</td>';
                    echo     
    '<td>'.number_format($products['price'][$x], 2).'</td>';
                    echo 
    '</tr>'
    Should be
    PHP Code:
                    echo '<tr>';
                    echo     
    '<td>'.$products[$x]['id'].'</td>';
                    echo     
    '<td><img src=img/'.$products[$x]['name'].'.png alt= />'.'</td>';
                    echo    
    '<td>'.$products[$x]['desc'].'</td>';
                    echo     
    '<td>'.number_format($products[$x]['price'], 2).'</td>';
                    echo 
    '</tr>'
    $x variable stand before column name is right. Can you say reason for it?

    Quote Originally Posted by Raju Gautam View Post
    Something like this using array_chunk would be best fit I think:
    Code php:
        echo "<pre>"; print_r(array_chunk($products, 5, true));
    Thank!

  11. #11
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,069
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by greedyman View Post
    $x variable stand before column name is right. Can you say reason for it?
    You created a multi-dimensional array, the index of each array is numeric, since you didn't specifically define a key, so it starts with 0 and goes to 15 (since you have 16 items).

    $products[0] would store the data for broccoli, $products[1] contains the data for butter, etc.

    Raju Gautam's implementation of array_chunk is a good solution, it does require changing your code entirely to what he posted, but it does work well for this.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  12. #12
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    You created a multi-dimensional array, the index of each array is numeric, since you didn't specifically define a key, so it starts with 0 and goes to 15 (since you have 16 items).

    $products[0] would store the data for broccoli, $products[1] contains the data for butter, etc.

    Raju Gautam's implementation of array_chunk is a good solution, it does require changing your code entirely to what he posted, but it does work well for this.
    I got it. My code is working normally. Once again thank a lot.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •