March 16, 2012

Displaying db rows through <ul> with equal or less <li>

Question by Nabeel Basheer

The following is a query results stored in $performset.

      $sqlperform = $ilance->db->query("
            SELECT
            q.title_per 
            FROM " . DB_PREFIX . "perform q
            LEFT JOIN " . DB_PREFIX . "perform_answers a ON (q.cid =  a.cid)
            WHERE a.user_id = '" . $res_vendor['user_id'] . "'
        ");
        if ($ilance->db->num_rows($sqlperform) > 0)
        {
            while ($rows = $ilance->db->fetch_array($sqlperform))
            {  
            $perform .='<li>'.$rows['title_per'].'</li>';  
                }
                      $performset .='<ul>'.$perform.'</ul>';
         }      

  echo   

The values i get from particular variable are
Adobe Flex, C++ Builder, C#/.Net, C/C++/Unix, C/C++/Win32SDK, Oracle DBA,
Data Entry,

The out put I have got form this is

Adobe Flex, C++ Builder, C#/.Net, C/C++/Unix, C/C++/Win32SDK, Oracle DBA, 
Data Entry, Oracle DBA, Adobe Flex ,C++ Builder, C#/.Net , Data Entry
C/C++/Unix, C/C++/Win32SDK

But the output I need is

Data Entry    C++ Builder   C/C++/Win32SDK
Oracle DBA    C#/.Net
Adobe Flex    C/C++/Unix

The html that works for this is as such

    <ul>
                            <li>Data Entry</li>
                            <li>Oracle DBA</li>
                            <li>Adobe Flex</li>
                        </ul>
                        <ul>
                            <li>C++ Builder</li>
                            <li>C#/.Net</li>
                            <li>C/C++/Unix</li>
                        </ul>

How can i script it in php to produce such n number of results

Answer by Starx

Use of array_chunk() is perfect in this case.

Take a looks at this example, might looks complicated but might be how you need

    if ($ilance->db->num_rows($sqlperform) > 0)
    {
        $drows = array();

        while($rows = $ilance->db->fetch_array($sqlperform)) {  
           $drows[] = $rows;
        }


        $chunks = array_chunk($drows, 3, true);       
        foreach($chunks as $chunk) {
          $set = "<ul>";
             foreach($chunk as $key => $value) {
                $set .= '<li>'.$value['title_per'].'</li>';  
             }
          $set .= "</ul>";
          echo $set;
        }

      }

TESTED, but For test, I used the following codes, which simulates the situation above

$ar1  = array(1,2,3); //db row
$ar2 = array($ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1,$ar1);

$drows = $ar2; //collection of db rows
$chunks = array_chunk($drows, 3, true);
foreach($chunks as $chunk) {
  $set = "<ul>";
     foreach($chunk as $key => $value) {
        $set .= '<li>'.$key.$value[0].'</li>';  
     }
  $set .= "</ul>";
  echo $set;
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!