Jump to content

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


terungwa
Go to solution Solved by adam_bray,

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>";
		
	};
Edited by terungwa
Link to comment
Share on other sites

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.

Edited by terungwa
Link to comment
Share on other sites

  • Solution

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>";
	}
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.