...

View Full Version : While loop problem



Mark-Jones
10-23-2012, 03:20 PM
How do I make this code work? At the moment it is displaying 2 results in the same line as can be seen here (http://ukshoppinghub.co.cc/files/2results-affforum.php):


<?php

// Make a MySQL Connection
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

$query = "SELECT * FROM minibbtable_posts INNER JOIN minibbtable_topics ON minibbtable_posts.topic_id=minibbtable_topics.topic_id where minibbtable_posts.post_text like '%audio%' and minibbtable_posts.forum_id = '9' limit 2 ";

$result = mysql_query($query) or die(mysql_error());
// Print out result

while($row = mysql_fetch_array($result)){
$str = $row['topic_title'];
$str=str_replace('0', '', $str);
$str=str_replace('1', '', $str);
$str=str_replace('2', '', $str);
$str=str_replace('3', '', $str);
$str=str_replace('4', '', $str);
$str=str_replace('5', '', $str);
$str=str_replace('6', '', $str);
$str=str_replace('7', '', $str);
$str=str_replace('8', '', $str);
$str=str_replace('9', '', $str);
$str=str_replace('0', '', $str);
$str=str_replace('&', '', $str);
$str=str_replace('pound;', '', $str);
$str=str_replace('-the', '', $str);
$str=str_replace('!', '', $str);
$str=str_replace(',', '', $str);
$str=str_replace('%', '', $str);
$str=str_replace(':', '', $str);
$str=str_replace('', '', $str);
$str=str_replace(' ', ' ', $str);
$str=str_replace(' ', '-', $str);
$str=str_replace('-and', '', $str);
$str=str_replace('.', '', $str);
$str=str_replace('(', '', $str);
$str=str_replace(')', '', $str);
$str=str_replace('*', '', $str);

$results .= "http://ukshoppinghub.co.cc/active-offers-9/$str-". $row['topic_id'] .".html\r\n";

$str2 .= $results;
$str2=str_replace('--', '', $str2 );
$str2 = strtolower($str2);

echo 'Forum: <a href="http://ukshoppinghub.co.cc/active-offers-9/">';
echo 'Active Offers</a>';
echo '<br>';
echo 'Title:<a target="_top" href="';
echo $str2;
echo '">';

$str2=str_replace('http://ukshoppinghub.co.cc/active-offers-9/', '', $str2);
$str2=str_replace('.html', '', $str2);

echo $str2;
echo '</a><br>';

echo 'Offer Active To: <b><font color="green">';
echo $row['topic_end'];
echo '</font></b>';
echo '<br>';

}
echo '<div style="height: 5px;"></div>';

$query2 = "SELECT * FROM minibbtable_posts INNER JOIN minibbtable_topics ON minibbtable_posts.topic_id=minibbtable_topics.topic_id where minibbtable_posts.post_text like '%audio%' and minibbtable_posts.forum_id = '1' limit 1 ";

$result2 = mysql_query($query2) or die(mysql_error());
// Print out result

while($row = mysql_fetch_array($result2)){

$str = $row['topic_title'];
$str=str_replace('0', '', $str);
$str=str_replace('1', '', $str);
$str=str_replace('2', '', $str);
$str=str_replace('3', '', $str);
$str=str_replace('4', '', $str);
$str=str_replace('5', '', $str);
$str=str_replace('6', '', $str);
$str=str_replace('7', '', $str);
$str=str_replace('8', '', $str);
$str=str_replace('9', '', $str);
$str=str_replace('0', '', $str);
$str=str_replace('&', '', $str);
$str=str_replace('pound;', '', $str);
$str=str_replace('-the', '', $str);
$str=str_replace('!', '', $str);
$str=str_replace(',', '', $str);
$str=str_replace('%', '', $str);
$str=str_replace(':', '', $str);
$str=str_replace('', '', $str);
$str=str_replace(' ', ' ', $str);
$str=str_replace(' ', '-', $str);
$str=str_replace('-and', '', $str);
$str=str_replace('.', '', $str);
$str=str_replace('(', '', $str);
$str=str_replace(')', '', $str);
$str=str_replace('*', '', $str);

$results2 .= "http://ukshoppinghub.co.cc/expired-offers-1/$str-". $row['topic_id'] .".html\r\n";

$str3 .= $results2;
$str3=str_replace('--', '', $str3);
$str3 = strtolower($str3);

echo 'Forum: <a href="http://ukshoppinghub.co.cc/expired-offers-1/">';
echo 'Expired Offers</a>';
echo '<br>';
echo 'Title:<a target="_top" href="';
echo $str3;
echo '">';

$str3=str_replace('http://ukshoppinghub.co.cc/expired-offers-1/', '', $str3);
$str3=str_replace('.html', '', $str3);

echo $str3;
echo '</a><br>';

echo 'Offer Expired: <b><font color="red">';
echo $row['topic_end'];
echo '</font></b>';
echo '<br>';
}

?>

As you can see the 2nd result is also showing the first in the same line. The more results the more end up on the same line?

Fou-Lu
10-23-2012, 03:49 PM
If you get all the data that you need on there, then PHP has done its job. Rendering is controlled by the HTML and CSS applied to the results. You do have errors here in that regards; these all need to be provided with valid markup and it looks to me that your source will not be valid (and has some old stuff like the font tag). These have to be placed in a valid container such as a div or p.
You should write a function for these things as well. These two blocks look identical in what they do. SQL guys may be able to suggest an alternative query as well; I'm not sure how easy it would be to combine them with the limits the way they are, but if they can be it'd be better to simply select only once.

As for repetition, that'll be caused by the use of concat. Initialize these variables for $str2 and $str3 where appropriate to prevent appending. I'd expect that you should simply do that in the first couple of lines of the while.

Mark-Jones
10-23-2012, 04:10 PM
If you get all the data that you need on there, then PHP has done its job. Rendering is controlled by the HTML and CSS applied to the results. You do have errors here in that regards; these all need to be provided with valid markup and it looks to me that your source will not be valid (and has some old stuff like the font tag). These have to be placed in a valid container such as a div or p.
You should write a function for these things as well. These two blocks look identical in what they do. SQL guys may be able to suggest an alternative query as well; I'm not sure how easy it would be to combine them with the limits the way they are, but if they can be it'd be better to simply select only once.

As for repetition, that'll be caused by the use of concat. Initialize these variables for $str2 and $str3 where appropriate to prevent appending. I'd expect that you should simply do that in the first couple of lines of the while.

Thanks

Yeah the code was and is a bit messy but I will get to that soon. The initial problem has been sorted now by removing the . from results and str. Not sure why it was there in the first place to be honest. I think that is what you meant by 'concat'?

Anyways thanks for the advice.

Fou-Lu
10-23-2012, 04:16 PM
Yes that's correct. In PHP, the . is the concat operator, so if you do $str .= 'value', it will keep appending (and create $str if it doesn't already exist and issue an E_NOTICE level error). Removing it off of the first assignment to it will create a new variable with just that value, and will overwrite any previous variable under that name.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum