MDanz Posted February 6, 2010 Share Posted February 6, 2010 how do i do .. if $keyword already echoed, don't repeat it. e.g. if $keyword is "Car" and it's echoed.. then a duplicate echo of "Car" shouldn't be echoed. Link to comment https://forums.phpfreaks.com/topic/191197-help-with-if-statement/ Share on other sites More sharing options...
jl5501 Posted February 6, 2010 Share Posted February 6, 2010 Store each word you echo in an array, then check it it not in the array before echoing Link to comment https://forums.phpfreaks.com/topic/191197-help-with-if-statement/#findComment-1008084 Share on other sites More sharing options...
MDanz Posted February 6, 2010 Author Share Posted February 6, 2010 can i store in an array, when results come from a loop?.. if so, how? i know with the array i can use array_unique for duplicate values, so that solves that. But i don't know how to implement an array into a loop. <?php mysql_connect("localhost", "Master", "pword"); mysql_select_db("db"); $letter = htmlentities($_GET['search']); $user = mysql_query("SELECT * FROM Stacks WHERE keywords LIKE '$letter%' ORDER BY keywords")or die (mysql_error()); while($rowz = mysql_fetch_array($user)){ $keyword = $rowz['keywords']; $name = $rowz['name']; $bad = mysql_query("SELECT * FROM Stacks WHERE keywords LIKE '$letter%' ORDER BY keywords")or die (mysql_error()); $num_rows = mysql_num_rows($bad)or die (mysql_error()); } if ($num_rows == 0) { echo "<font face='Courier New' font size=18px font color=#FF9900>$letter</font><br /><br />"; echo "<font face='Courier New' font size=3px font color=#FBB917>No Stacks</font><br>"; } else { echo "<font face='Courier New' font size=18px font color=#FF9900>$letter</font><br /><br />"; echo "<font face='Courier New' font size=3px font color=#FBB917><a href='stack.php?search=$keyword&submit=Go!' style='text-decoration: none';>$keyword</a></font><br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/191197-help-with-if-statement/#findComment-1008086 Share on other sites More sharing options...
jl5501 Posted February 6, 2010 Share Posted February 6, 2010 Set this up before your while $words_output = array(); then the else half of your loop looks like else { if(!in_array($keyword,$words_output)) { $words_output[] = $keyword; echo "<font face='Courier New' font size=18px font color=#FF9900>$letter</font><br /><br />"; echo "<font face='Courier New' font size=3px font color=#FBB917><a href='stack.php?search=$keyword&submit=Go!' style='text-decoration: none';>$keyword</a></font><br>"; } } Link to comment https://forums.phpfreaks.com/topic/191197-help-with-if-statement/#findComment-1008088 Share on other sites More sharing options...
MDanz Posted February 6, 2010 Author Share Posted February 6, 2010 thanks works perfectly. Link to comment https://forums.phpfreaks.com/topic/191197-help-with-if-statement/#findComment-1008091 Share on other sites More sharing options...
MDanz Posted February 6, 2010 Author Share Posted February 6, 2010 is there a way to remove the duplicate result from the loop if the same result is there already?.. what i'm getting is i want to display 10 results. Say 3 of those results are duplicate. Only 8 results are now displayed... the other two are "hidden"... any way around this? array_unique? Link to comment https://forums.phpfreaks.com/topic/191197-help-with-if-statement/#findComment-1008099 Share on other sites More sharing options...
MDanz Posted February 6, 2010 Author Share Posted February 6, 2010 it's because of this line... "SELECT * FROM Stacks ORDER BY id DESC LIMIT 9"; it hides duplicate values but it is still counted in the loop. So if there is 20 results in mysql database. the first 5 results are the same value. then it will show 4 result... Link to comment https://forums.phpfreaks.com/topic/191197-help-with-if-statement/#findComment-1008105 Share on other sites More sharing options...
jl5501 Posted February 6, 2010 Share Posted February 6, 2010 Depending on what fields you want output you need to do this, assuming you want an id field select distinct id from stacks order by id desc limit 9 Link to comment https://forums.phpfreaks.com/topic/191197-help-with-if-statement/#findComment-1008110 Share on other sites More sharing options...
MDanz Posted February 6, 2010 Author Share Posted February 6, 2010 now it only displays 1 result? ... confusing if i use distinct without the array will it display no duplicate results still? Link to comment https://forums.phpfreaks.com/topic/191197-help-with-if-statement/#findComment-1008120 Share on other sites More sharing options...
MDanz Posted February 7, 2010 Author Share Posted February 7, 2010 nvm Link to comment https://forums.phpfreaks.com/topic/191197-help-with-if-statement/#findComment-1008125 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.