Jump to content

fetch data from db - display as a menu - group in headers


cinek

Recommended Posts

I have a menu on the left of the site. That menu has a header (multiple headers) for e.g. company. Each header should contain e.g. name of their employees. How can I do that in php?I don't want to hard code the amount of companies as adding 1 to the database would mean editing the html etc (unless it's not possible)this is the html I have as an example of what it should (more or less) look like

<div id="menu">							<span class="category1"><</span>					<div id="cat1">					<ul>				<li>name</li>				<li>name</li>				<li>name</li>			</ul>					</div>							<span class="category2">Cat2</span>					<div id="cat2">			<ul>				   <li>name</li>				<li>name2</li>				<li>name3</li>				  			</ul>						</div>			 		<span class="category3">Cat3</span>			 		<div id="cat3">			   <ul>				  <li>name1</li>				<li>name</li>				<li>name3</li>			 </ul>		 </div>			 					</div><!-- End menu -->

how would I fill this out with php? Again, this is an example, I don't want to hard code the amount of categories in.... each category might have from 1 to lets say 20 employeestried something like this but it just echos out all companies with names and doesn't group them

while($row = mysql_fetch_array($company))				{					echo '<span class="'.$row['Company']. '">'.$row['Company'].'</span>';					echo '<div id="'.$row['Company']. '"></span>';					echo '<ul>';					echo '<li>' . $row['Name'] . '</li>';					echo '</ul>';				}

this is what it looks like

company1 name1company2 name1company1 name2company2 name2
it should be like this:
company1 name1 name2 etccompany2 name1 name2 etc
this is the query
$query = "SELECT Company, Name FROM info";		$company = mysql_query($query);

fields in the database are:idCompanyName Status

Link to comment
Share on other sites

You can get the number of rows returned from a query with mysql_num_rows(). This would let you do pretty much everything you're asking for - instead of hardcoing the number, get it from there.

Link to comment
Share on other sites

how would I do that? wouldn't this mean I have to define the company name to get the num rows? as the data is not in orderso in the db you might haveCompany->Name________________comp1 ->sdfcomp1->sdafcomp1->sdafcomp2->safdzcomp3->sadfsfadscomp1->sdffdsacomp2->asfdetc

Link to comment
Share on other sites

In that case, it's probably best that you order the stuff properly (by company in this case), and then simply generate the first category while you're getting values that belong to it, i.e.

$query = "SELECT Company, Name FROM info ORDER BY Company, Name"; $company = mysql_query($query);$currentCompany = '';while($row = mysql_fetch_array($company)){	//If we've now switched to another company...	if ($row['Company'] != $currentCompany) {		//... and it's not the very first company...		if ($currentCompany != '') {			//...we'll be finishing up the list we started generating and...			echo '</ul></div>';		}		//... we'll start a new list.		echo '<span class="'.$row['Company']. '">'.$row['Company'].'</span>';		echo '<div id="'.$row['Company']. '">';		echo '<ul>';		//And make the company switch for the sake of the next iteration.		$currentCompany = $row['Company'];	}	//But regardless of whether the company has changed or not, we're in a list already, so we might as well just fill it.	echo '<li>' . $row['Name'] . '</li>';}echo '</ul></div>';

Link to comment
Share on other sites

Opps... my bad. Forgot to switch the $currentCompany appropriately. I've modified the code above. Try it now.

Link to comment
Share on other sites

You need to at the closing div not just at the very end of the list, but at the end of every category.I've modified the code again. See if you can spot where I've also added the closing div tag.

Link to comment
Share on other sites

I'm trying to add an image depending on the field value in the database. I'm trying something like this:

// check status	if($row['Status'] === "Online"){		//But regardless of whether the company has changed or not, we're in a list already, so we might as well just fill it.		echo '<li><img src="images/green.png" alt="online" />' . $row['Name'] . '</li>';	} else {		echo '<li><img src="img/red.png alt="offline" />'.$row['name'] . '</li>';	}

instead of this:

//But regardless of whether the company has changed or not, we're in a list already, so we might as well just fill it.	echo '<li>' . $row['Name'] . '</li>';

but it only returns one row;/how can I fix this?

Link to comment
Share on other sites

// check statuswhile($row=mysql_fecth_array($company)){	if($row['Status'] === "Online"){		//But regardless of whether the company has changed or not, we're in a list already, so we might as well just fill it.		echo '<li><img src="images/green.png" alt="online" />' . $row['Name'] . '</li>';	} else {		echo '<li><img src="img/red.png alt="offline" />'.$row['name'] . '</li>';	}}

you may put in under while loop

Link to comment
Share on other sites

I'd assume you've also added Status to your query, like:

$query = "SELECT Company, Name, Status FROM info ORDER BY Company, Name";

And that this field is a (var)char field which has exactly the value "Online" (and not "online", "ONLINE", etc.) when the company/name is online.(sorry for asking the obvious, but I've witnessed people miss the obvious too many times)I've also noticed that you're using "name" on the else part, and that your image path is also very different, and produces invalid (X)HTML (missing a quote)... you might instead want to use this:

 //But regardless of whether the company has changed or not, we're in a list already, so we might as well just fill it. // check status if($row['Status'] === "Online"){   echo '<li><img src="images/green.png" alt="online" />' . $row['Name'] . '</li>'; } else {   echo '<li><img src="images/red.png" alt="offline" />' . $row['Name'] . '</li>'; }

Link to comment
Share on other sites

I'd assume you've also added Status to your query, like:
$query = "SELECT Company, Name, Status FROM info ORDER BY Company, Name";

And that this field is a (var)char field which has exactly the value "Online" (and not "online", "ONLINE", etc.) when the company/name is online.(sorry for asking the obvious, but I've witnessed people miss the obvious too many times)I've also noticed that you're using "name" on the else part, and that your image path is also very different, and produces invalid (X)HTML (missing a quote)... you might instead want to use this:

 //But regardless of whether the company has changed or not, we're in a list already, so we might as well just fill it. // check status if($row['Status'] === "Online"){   echo '<li><img src="images/green.png" alt="online" />' . $row['Name'] . '</li>'; } else {   echo '<li><img src="images/red.png" alt="offline" />' . $row['Name'] . '</li>'; }

thanks! not sure how I missed that. Yes it's "Online" and I have appended the query :)thanks again :)
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...