View Full Version : Array Issues
Kirk Y
05-28-2006, 11:53 PM
I've made a really simple shoutbox, so that administrators on my forum can put up important messages for all the members to see, just in case they don't browse the forums. This is the code I'm using to pull the posts from my database:
$blast = $db->query_first("SELECT * FROM blastmsg ORDER BY blastid DESC");
Then I use $blast[message] to post the message. The problem is that it's only posting the newest message -- I want it to post all of the messages, or at the very least 3. How should I go about doing this?
Hellcat
05-29-2006, 12:12 AM
Use query_read() instead of query_first() and alter the SQL statement to get all "shouts" you want (like the last 3 or whatever).
Then loop through the result with fetch_array() to get the returned rows and post the messages one by one.
Kirk Y
05-29-2006, 12:20 AM
$blast_msg = $db->query_read("SELECT * FROM blastmsg ORDER BY blastid DESC");
if (!$db->num_rows($blast_msg))
{
$blasterr = "<div align=center><i>There are no blast messages!</i></div>";
}
else
{
while ($blast = $db->fetch_array($blast_msg))
{
$user = $blast[user];
$date = $blast[date];
$message = $blast[message];
}
}
I tried this also -- is this what you're talking about? Either way, this one didn't work either.
Should I try this instead:
$blast_msg = $db->query_read("SELECT * FROM blastmsg ORDER BY blastid DESC LIMIT 0,3");
Adrian Schneider
05-29-2006, 01:13 AM
Try something like this: $blast_msg = $db->query_read("
SELECT *
FROM blastmsg
ORDER BY blastid DESC
LIMIT 3
");
if (!$db->num_rows($blast_msg))
{
$blasterr = '<div align="center"><em>There are no blast messages!</em></div>';
}
else
{
while ($blast = $db->fetch_array($blast_msg))
{
$user = $blast['user'];
$date = $blast['date'];
$message = $blast['message'];
// Here is where you display the current message (ex)...
$blasterr .= "<div>[$date] $user: $message</div>";
}
}
I really only added the single quotes for string array keys, and adding to the $blasterr variable with each loop. The way you have it now $user, $date and $message will just be whatever they were last, you want to display (or add to a variable) every time so they are unique.
Kirk Y
05-29-2006, 01:24 AM
Thanks SirAdrian -- all working fine, it's much appreciated.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.