Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    While loop problem

    How do I make this code work? At the moment it is displaying 2 results in the same line as can be seen here:
    Code:
    <?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?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    Mark-Jones (10-23-2012)

  • #3
    New Coder
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •