Jump to content

why doesn't this work?


aebstract

Recommended Posts

This doesn't work:

if ($r[topic_id] != $used_ids[1] || $r[topic_id] != $used_ids[2] || $r[topic_id] != $used_ids[3] || $r[topic_id] != $used_ids[4]){

If I just put:

if ($r[topic_id] != $used_ids[1]){

 

The '||' is allowed to be used like this, right? I'm probably doing this a lot harder of a way than what is needed. Here is everything that is going on:

 


$query = mysql_query("
SELECT *
FROM posts
JOIN forums ON ( forums.forum_id = posts.forum_id )
JOIN users ON ( users.user_id = posts.poster_id )
ORDER BY post_time DESC
");




if (mysql_num_rows($query)!=0){
$recent_posts = '';
$count = '1';
    $used_ids = '';
	while($r=mysql_fetch_array($query))
	{


$used_ids .= " $r[topic_id]";
$used_ids = explode(" ", $used_ids);

		if ($r[topic_id] != $used_ids[1] || $r[topic_id] != $used_ids[2] || $r[topic_id] != $used_ids[3] || $r[topic_id] != $used_ids[4]){

				$query2 = mysql_query("SELECT post_subject FROM posts WHERE post_id = $r[topic_id]");

					if (mysql_num_rows($query)!=0){
						while($s=mysql_fetch_array($query2))
						{
						$topic_subject = $s[post_subject];
						}

					}



					if ($count != 4) { $recent_posts .= "<table class=\"recent_posts_table\">"; } else { $recent_posts .= "<table class=\"recent_posts_table_2\">"; }

				$recent_posts .= "<tr><td class=\"r_p_icon\">
				<img src=\"images/recent_post.png\" />
				</td><td class=\"r_p_subject\">
				<span class=\"r_p_link\"><a href=\"forums/viewtopic.php?f=$r[forum_id]&t=$r[topic_id]\">$topic_subject</a></span>
				</td><td class=\"r_p_spacer\">
				</td><td>
				<table><tr><td>
				<span>by <a href=\"forums/memberlist.php?mode=viewprofile&u=$r[user_id]\">$r[username]</a>: about fourty minutes ago</span>
				</tr><tr>
				In <span class=\"r_p_link\"><a href=\"forums/viewforum.php?f=$r[forum_id]\">$r[forum_name]</a></span> >> <span class=\"r_p_link\"><a href=\"forums/viewtopic.php?f=$r[forum_id]&t=$r[topic_id]#p$r[post_id]\">$r[post_subject]</a></span>
				</tr></td></table>
				</td></tr>
				</table>
				";
			$count++;

		}

$used_ids = implode(" ", $used_ids);

	}

}





 

Basically the parts about $used_ids is what we're looking at. I'm trying to pull out posts from the database, but I only want to display a maximum of 4 results, and those results need to have unique topic_id's. So I was trying to store them as I go through them, check if it is stored and if it is.. don't display the line, go to the next. Check it's topic_id and so on.

Link to comment
https://forums.phpfreaks.com/topic/193146-why-doesnt-this-work/
Share on other sites

array indexes of assosiative arrays, need to be quoted, unless you are using defined constants.

like $r['topic_id']

 

I dunno, I can do this:

echo "$r[topic_id]";

and it doesn't have a problem echoing the correct number.

 

Also, like I said.. If I don't use the || in my if statement, it will work in blocking out any from the first grabbed row. When I throw the || in to try and check against those values as well, it doesn't block anything anymore. I hope I'm explaining this well enough.

This seems to work:

 


$query = mysql_query("
SELECT *
FROM posts
JOIN forums ON ( forums.forum_id = posts.forum_id )
JOIN users ON ( users.user_id = posts.poster_id )
ORDER BY post_time DESC
");




if (mysql_num_rows($query)!=0){
$recent_posts = '';
$count = '1';
    $used_ids = array(0);
	while($r=mysql_fetch_array($query))
	{


if (!isset($used_ids[4])) {
if ($used_ids[4] != $r[topic_id]) {
if ($used_ids[3] != $r[topic_id]) {
if ($used_ids[2] != $r[topic_id]) {
if ($used_ids[1] != $r[topic_id]) {

				$query2 = mysql_query("SELECT post_subject FROM posts WHERE post_id = $r[topic_id]");

					if (mysql_num_rows($query)!=0){
						while($s=mysql_fetch_array($query2))
						{
						$topic_subject = $s[post_subject];
						}

					}



					if ($count != 4) { $recent_posts .= "<table class=\"recent_posts_table\">"; } else { $recent_posts .= "<table class=\"recent_posts_table_2\">"; }

				$recent_posts .= "<tr><td class=\"r_p_icon\">
				<img src=\"images/recent_post.png\" />
				</td><td class=\"r_p_subject\">
				<span class=\"r_p_link\"><a href=\"forums/viewtopic.php?f=$r[forum_id]&t=$r[topic_id]\">$topic_subject</a></span>
				</td><td class=\"r_p_spacer\">
				</td><td>
				<table><tr><td>
				<span>by <a href=\"forums/memberlist.php?mode=viewprofile&u=$r[user_id]\">$r[username]</a>: about fourty minutes ago</span>
				</tr><tr>
				In <span class=\"r_p_link\"><a href=\"forums/viewforum.php?f=$r[forum_id]\">$r[forum_name]</a></span> >> <span class=\"r_p_link\"><a href=\"forums/viewtopic.php?f=$r[forum_id]&t=$r[topic_id]#p$r[post_id]\">$r[post_subject]</a></span>
				</tr></td></table>
				</td></tr>
				</table>
				";
			$count++;



		array_push($used_ids, $r[topic_id]);
}
}
}
}
}


	}

}

 

Not sure if it's the neatest or best way to do it, if anyone has a suggestion I'm more than willing to give it a try. If not, at least this should be working now. :)

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.