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

Link to comment
Share on other sites

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>
<?
}
?>

Link to comment
Share on other sites

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);

?>

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.