Jump to content

how to display one heading using for loop


shadd
Go to solution Solved by mac_gyver,

Recommended Posts

I have this  resultset :

pId Duration emdate Sub SubId   Lvl LvId
1.1 null null basic lug 1 ule 1
1.2 null null basic lug 1 ule 1
1.2 null null ger 2 ace 1

I would like to have it display like so in a <ul>:

ULE

BASIC LUG

BASIC LUG

ACE

ger

how can I achieve that if each is a row from database resultset.

I have tried ;

    foreach($ScheduledExamlist as $Scheduledexam) {
        echo '<li class="current"><a href="#">'.$Scheduledexam['Lvl'].'</a></li>' ;
        echo '<li>'.$Scheduledexam.'</li>';

    }

but heading  appears twice

Edited by shadd
clarity
Link to comment
Share on other sites

10 minutes ago, jodunno said:

where is your database code? it would be nice to see your query in addition to expected results.

here is the array that i need to print out in a <ul> :

array ( 0 => array ( 'pId' => '1.1', 'Duration' => NULL, 'emdate' => NULL, 'Sub' => 'BASIC lug', 'SubId' => 1, 'Lvl' => ' ULE', 'LvId' => 1, ), 1 => array ( 'pId' => '1.2', 'Duration' => NULL, 'emdate' => NULL, 'Sub' => 'BASIC lug', 'SubId' => 1, 'Lvl' => ' ULE', 'LvId' => 1, ), 2 => array ( 'pId' => '1.3', 'Duration' => NULL, 'emdate' => NULL, 'Sub' => 'ger', 'SubId' => 1, 'Lvl' => ' ACE', 'LvId' => 2,  ) )

iam expecting to get this result;

ULE

BASIC LUG

BASIC LUG

ACE

ger

Link to comment
Share on other sites

29 minutes ago, mac_gyver said:

you need to index/pivot the data when you fetch it, using the Lvl value as the main array index. you can then simply loop over the data using two nested foreach loops to produce the output, like was shown in your previous similar thread.

can you please, show your concept  on that array using php

Edited by shadd
Link to comment
Share on other sites

  • Solution

assuming you are using the PDO extension, it has a fetch mode that will index/pivot the data for you -

// build and execute the query here...

// index/pivot the fetched data using the first column in the SELECT list
$data = $stmt->fetchAll(PDO::FETCH_GROUP);


// at the point of producing the output
if(!$data)
{
	echo "<p>There is no data to display</p>";
}
else
{
	foreach($data as $index=>$items)
	{
		// output the section heading
		echo "<li class='current'><a href='#'>$index</a></li>\n";
		// loop over the data in the section
		foreach($items as $row)
		{
			// output each row of data
			echo "<li>{$row['Sub']}</li>\n";
		}
		// if needed, close the section here...
	}
}

 

Link to comment
Share on other sites

... or ...

    $res = $pdo->query("SELECT Lvl
                             , Sub
                        FROM shadd_1
                        ORDER BY Lvl DESC, Sub
                        ");
    $results = $res->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_COLUMN);
    
    foreach ($results as $lvl => $arr)  {
        echo "$lvl <UL>";
        foreach ($arr as $sub)  {
            echo "<li>$sub</li>";
        }
        echo "</ul>";
    }

FYI - the $results array (slightly simpler than the $data array in above post) looks like this..

Array
(
    [ULE] => Array
        (
            [0] => BASIC lug
            [1] => BASIC lug
        )

    [ACE] => Array
        (
            [0] => ger
        )

)

 

  • Like 1
Link to comment
Share on other sites

1 hour ago, Barand said:

... or ...

    $res = $pdo->query("SELECT Lvl
                             , Sub
                        FROM shadd_1
                        ORDER BY Lvl DESC, Sub
                        ");
    $results = $res->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_COLUMN);
    
    foreach ($results as $lvl => $arr)  {
        echo "$lvl <UL>";
        foreach ($arr as $sub)  {
            echo "<li>$sub</li>";
        }
        echo "</ul>";
    }

FYI - the $results array (slightly simpler than the $data array in above post) looks like this..

Array
(
    [ULE] => Array
        (
            [0] => BASIC lug
            [1] => BASIC lug
        )

    [ACE] => Array
        (
            [0] => ger
        )

)

 

correct and simple but,is it possible to add a key to the results array like so:

foreach ($results as $lvl => $arr) { echo "$lvl <UL>"; foreach ($arr as $sub) { $results['go_to_exam'] ="dear";echo "<li>$sub</li>"; } echo "</ul>"; }

in a for loop this could work:

for ($i = 0; $i < count($results); $i++)
		{
			$results[$i]['go_to_exam'] =
			"dear".$i;
		}

if i had used  fetch:assoc

Edited by shadd
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.