Jump to content

Dynamically display Parent & Child Lists in a pricing table ('while' and 'foreach' loops)


terungwa

Recommended Posts

I wish to develop a dynamic pricing table that lists all the parent elements as headings, and then underneath that the children elements. While my current design prints out the pricing table, I realised that with each new entry I make in the tables, I have to manually update the children element list in the while loop (in my pricing.php script).

 

I need the loops to handle that updating dynamically without human interference. And I am looking forward to suggestions on how to achieve this.

 

Thanks.

 

Below is my mysql table scheme and pricing.php script respectively:

--
-- Table structure for table `programmes`
--
CREATE TABLE IF NOT EXISTS `programmes` (
  `programme_id` int(11) NOT NULL AUTO_INCREMENT,
  `programme` varchar(10) NOT NULL,
  PRIMARY KEY (`programme_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Table structure for table `programme_features`
--

CREATE TABLE IF NOT EXISTS `programme_features` (
  `feature_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `programme_id` int(11) unsigned NOT NULL,
  `cost` decimal(10,2) NOT NULL,
  `course_desc` varchar(255) NOT NULL,
  `benefit_1` varchar(60) NOT NULL,
  `benefit_2` varchar(255) NOT NULL,
  `join_now` varchar(100) NOT NULL,
  PRIMARY KEY (`feature_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;


PRICING.PHP: And below are the 'while and for each loops'

//mysql connection

$level_type = array();

$sql = "SELECT programmes.`programme_id`, `programme`, `feature_id`, `cost`, `course_desc`, `benefit_1`, `benefit_2`, `join_now` FROM programme_features
INNER JOIN programmes
ON programmes.programme_id = programme_features.programme_id
ORDER BY programme ASC";
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc())
	{
		$level_type[$row['programme']][]=$row['cost'];
		$level_type[$row['programme']][]=$row['course_desc'];
		$level_type[$row['programme']][]=$row['benefit_1'];
		$level_type[$row['programme']][]=$row['benefit_2'];
		$level_type[$row['programme']][]=$row['join_now'];
	}

<h1>Our Courses:</h1>

foreach($level_type AS $level => $programme_features)
	{
		echo "<div class='pricing-table' id='pricing-table'>";
			echo "<h3>{$level}</h3>";
			echo "<ul>";
				foreach($programme_features AS $attribute)
				{
					echo "<li>{$attribute}</li>";
				}		
			echo "</ul>";
		echo "</div>";
		
	};

Do  $level_type[$row['programme']] = $row;  in the while loop

 

Then change the foreach loop to

				foreach($programme_features AS $attribute_name => $attribute_value)
				{
					echo "<li>{$attribute_value}</li>";
				}

Thanks @Ch0cu3r . There is a little problem with the foreach loop you proposed. The foreach loop is still printing the parent element in the children list as well as the programme_id and feature_ids.

Take a look at how it looks at this url: http://portal.virtualweb.com.ng/pricing.php

I have modified the select query so as to remove the  programme_id and feature_id from the $programme_features array :

"SELECT `programme`, `cost`, `course_desc`, `benefit_1`, `benefit_2`, `join_now` FROM programme_features
INNER JOIN programmes
ON programmes.programme_id = programme_features.programme_id
ORDER BY programme ASC"

And this is how it looks. (http://portal.virtualweb.com.ng/pricing.php).

However the parent element is still appearing in the children list.

 

Thanks.

Normally I'd start debugging by seeing what the array is doing (print_r()), then work out why it's doing that.

 

Try this -

foreach($programme_features AS $attribute_name => $attribute_value)
{
	if( $attribute_name != 'programme' )
	{
		echo "<li>{$attribute_value}</li>";
	}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.