Jump to content

[SOLVED] Help needed with database list


studgate

Recommended Posts

Hi guys,

  I am trying to solve a little problem. I have built a simple blog with name, content, title, time.

i have a list in the bottom of each page and I also have the list in the bottom of the blog page.

In the blog page, what I want to do is check the bottom list id and if the blog id is equals to the any of the bottom's

id, i want to highlight the one in the list.

Thank you in advance!  ;D

 

Link to comment
https://forums.phpfreaks.com/topic/89412-solved-help-needed-with-database-list/
Share on other sites

Your query is not very clear, it would be VERY helpful if you provided the two queries you are currently using to generate those two lists. I would look for a solution to modify the query of the list you want to use the highlighting in to add an additional column to identify if it should be highlighted or not.

This is the list in the bottom

<?
// generate and execute query
$query = "SELECT id, title, timestamp FROM blog ORDER BY timestamp DESC LIMIT 0, 4";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

// if records present
if (mysql_num_rows($result) > 0)
{
// iterate through resultset
// print article titles
while($row = mysql_fetch_object($result))
{
?>
	<li><a href="blog.php?id=<? echo $row->id; ?>"><span><? echo $row->slug; ?></span><em><? echo formatDate($row->timestamp); ?></em></a></li>
<?
}
}
// if no records present
// display message
else
{
?>
<font size="-1">No Blog Entry currently available</font>
<?
}

// close database connection
mysql_close($connection);
?>

 

And the blog page entry:

<?
// generate and execute query
$query = "SELECT title, content, contact, timestamp FROM blog WHERE id = '$id'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

// get resultset as object
$row = mysql_fetch_object($result);

// print details
if ($row)
{
?>

    <h2><? echo $row->title; ?></h2>	
<p><? echo nl2br($row->content); ?></p>

<p><font size="-2">This blog entry was published on <? echo formatDate($row->timestamp); ?>. For more information, please contact <? echo $row->contact; ?></font></p>
<?
}
else
{
?>
<p>
<font size="-1">That blog entry could not be located in our database.</font>
<?
}
?>

Don't use the php short tags they are not supported on all servers. Also the FONT tag has been depricated for many years, use styles.

 

this should do what you are asking for:

 

<?php

// generate and execute query
$query = "SELECT id, title, timestamp FROM blog ORDER BY timestamp DESC LIMIT 0, 4";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

if (mysql_num_rows($result) > 0)
{
// records present
// iterate through resultset
// print article titles
while($row = mysql_fetch_object($result))
{
	$span_style = ($row->id==$id) ? ' style="background-color:#FFFF00;"' : '';
	echo "<li><a href=\"blog.php?id={$row->id}\">";
	echo "<span {$span_style}>{$row->slug}</span><em>".formatDate($row->timestamp)."</em>";
	echo "</a></li>";
}
}
else
{
// no records present
// display message
echo "<font size=\"-1\">No Blog Entry currently available</font>";
}

// close database connection
mysql_close($connection);

?>

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.