• Limiting Items PHP from MySQL database

    Miranda65M Member

    Need help, It suppose to limit the items to show and it must get the data from the table row, but I don’t know why it kept showing all items.

    for ($i = 0; $i < count($structure); $i++)
    {
    if ($structure[$i]['parent_type'] == 'ItemBlock' && $GLOBALS['BLOCK_LIST']->getBlockById($structure[$i]['parent_id'])->getNumberOfItems())
    {
    $pos = $i;
    $itemBlockId = $structure[$i]['parent_id'];
    
    
    $items = array();
    
    
    $items[] = $structure[$i];
    
    
    while (isset($structure[++$i]) && $structure[$i]['parent_id'] == $itemBlockId)
    
    
       {
    
    
       $items[] = $structure[$i];
    
    
       $found = true;
    
    
    }
    
    
    if($found)
    
    
    {
    
    
       $noi = array();
    
    
    /** Call from MySQL **/
    
    
       $db = $GLOBALS['dao']->getConnection();
    
    
       $sql = 'SELECT number_of_items FROM '.DB_PREFIX.'item_blocks WHERE id = ?';
    
    
       $res = $db->query($sql, array($i));
    
    
       while($row = $res->fetchRow())
    
    
       {
    
    
    
    $noi[] = $row['number_of_items'];
    
    
       }
    
    
       $i--;
    
    
       //return $noi;
    
    
       $items = $noi;
    
    
    }
    
    
    array_splice($structure, $pos, $items);
    
       }
    
    }
    
  • SapnaVishwas Member

    A very easy fix would be to add LIMIT 0, 1 to your query. This makes sure you only retrieve one item from the database.

    $sql = 'SELECT number_of_items FROM '.DB_PREFIX.'item_blocks LIMIT 1'; 
  • Abhey Member

    Well one way to do so would be something like:

    //This depends on what you want to select of course.
       $sql = 'SELECT number_of_items FROM '.DB_PREFIX.'item_blocks WHERE id = IN(1, 2, 3, 4, 6, 9)';
    
       $res = $db->query($sql, array($i));
       while($row = $res->fetchRow())
       {
             //Do something with your data
       }
    

    With MySQL’s IN() function, you can pass a number of (in your case) ID’s, which will all be fetched. I think this is the best solution for you.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.