Hi, I'm new here, I'm currently doing my final year project & I'm stuck at displaying multiple results Here's my code for($i = 0; $i < sizeof($arrWords); $i++) { //echo $arrWords[0]; $query = "select articleids from searchwords where word = '{$arrWords[$i]}'"; $result = mysql_query($query); if(mysql_num_rows($result) > 0) { // Get the id's of the articles $row = mysql_fetch_array($result); $arrIds = explode(",", $row[0]); $arrWhere = implode( $arrIds); $aQuery = "select title, left(description, 100) as remedy from articles where articleid = $arrWhere[$i] "; $aResult = mysql_query($aQuery); $count = 0; $articles = array(); if(mysql_num_rows($aResult) > 0) { while($aRow = mysql_fetch_array($aResult)) { $articles[$count] = array ( "articleid" => $aRow["articleid"], "title" => $aRow["title"], "remedy" => $aRow["remedy"] ); $count++; } } if(isset($articles)) { $articles = array_unique($articles); echo "<h1>" . sizeof($articles); echo (sizeof($articles) == 1 ? " article" : " articles"); echo " found:</h1>"; foreach($articles as $a => $value) { ?> <a href="article.php?articleId=<?php echo $articles[$a]["articleid"]; ?>"> <b><u><?php echo $articles[$a]["title"]; ?></u></b> </a> <br><?php echo $articles[$a]["remedy"] . "..." ; ?> <br> <a href="article.php?articleId=<?php echo $articles[$a]; ?>"> http://www.mysite.com/article.php?articleId=<?php echo $articles[$a]["articleid"]; ?> </a> <br><br> <?php } } else { echo "No results found for '$search_keywords'<br>"; echo "<a href='searchdocs.php'>Go Back</a>"; } } mysql_close(); } PHP: My database consists of 2 tables: - "articles" which contains the articles with fields "articleid", "title", "description" & "remedy" - "searchwords" which contains keywords for searching the articles with fields "wordid", "word" & "articleids". So my problem is when I try to key in a keyword, only 1 result has been returned instead of multiple results. Can anyone help me on this. Thanks in advance. I hope I didn't break any rules here
If you will give print at each sql line, you will see why it get only one results against keywords you type.
$aQuery = "select title, left(description, 100) as remedy from articles where articleid in (select articleids from searchwords where word = '{$arrWords[$i]}')"; Code (markup): its easier to combine your queries into 1 single statement
Thank you for replying. I have tried your coding & it returned an error line 76 is referring to if(mysql_num_rows($result) > 0) PHP:
Is your code written this way? for($i = 0; $i < sizeof($arrWords); $i++) { $aQuery = "select title, left(description, 100) as remedy from articles where articleid in (select articleids from searchwords where word = '{$arrWords[$i]}')"; $aResult = mysql_query($aQuery); $count = 0; $articles = array(); if(mysql_num_rows($aResult) > 0) { while($aRow = mysql_fetch_array($aResult)) { $articles[$count] = array ( "articleid" => $aRow["articleid"], "title" => $aRow["title"], "remedy" => $aRow["remedy"] ); $count++; } } if(isset($articles)) { $articles = array_unique($articles); echo "<h1>" . sizeof($articles); echo (sizeof($articles) == 1 ? " article" : " articles"); echo " found:</h1>"; foreach($articles as $a => $value) { ?> <a href="article.php?articleId=<?php echo $articles[$a]["articleid"]; ?>"> <b><u><?php echo $articles[$a]["title"]; ?></u></b> </a> <br><?php echo $articles[$a]["remedy"] . "..." ; ?> <br> <a href="article.php?articleId=<?php echo $articles[$a]; ?>"> http://www.mysite.com/article.php?articleId=<?php echo $articles[$a]["articleid"]; ?> </a> <br><br> <?php } } else { echo "No results found for '$search_keywords'<br>"; echo "<a href='searchdocs.php'>Go Back</a>"; } } mysql_close(); } Code (markup):
^ thanx for the help but it still returned only 1 result maybe i should post the whole coding for better understanding? <?php $submit = $_POST["submit"]; $keywords = $_POST["keywords"]; if(isset($submit) || isset($keywords)) { doSearch($keywords); } else { getKeywords(); } function getKeywords() { ?> <html> <head> <title> Enter Search Keywords </title> </head> <body bgcolor="#FFFFFF"> <form name="frmKW" action="searchdocs.php" method="post"> <h1>Keyword Search</h1> Enter keywords to search on: <input type="text" name="keywords" maxlength="100"> <br><br><input type="submit" name="submit" value="Search"> </form> </body> </html> <?php } function doSearch($search_keywords) { $arrWords = explode(" ", $search_keywords); if(sizeof($arrWords) == 0 || $search_keywords == "") { echo "You didn't enter any keywords<br>"; echo "<a href='searchdocs.php'>Go Back</a>"; } else { // Connect to the database $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ailment", $con); for($i = 0; $i < sizeof($arrWords); $i++) { //codes by javaongsan $aQuery = "select title, left(description, 100) as remedy from articles where articleid in (select articleids from searchwords where word = '{$arrWords[$i]}')"; $aResult = mysql_query($aQuery); $count = 0; $articles = array(); if(mysql_num_rows($aResult) > 0) { while($aRow = mysql_fetch_array($aResult)) { $articles[$count] = array ( "articleid" => $aRow["articleid"], "title" => $aRow["title"], "remedy" => $aRow["remedy"] ); $count++; } } if(isset($articles)) { $articles = array_unique($articles); echo "<h1>" . sizeof($articles); echo (sizeof($articles) == 1 ? " ailment(s)" : " articles"); echo " found:</h1>"; foreach($articles as $a => $value) { ?> <a href="list ailment.php<?php echo $articles[$a]["articleid"]; ?>"> <b><u><?php echo $articles[$a]["title"]; ?></u></b> </a> <br><?php echo $articles[$a]["remedy"] . "..." ; ?> </a> <br><br> <?php } } else { echo "No results found for '$search_keywords'<br>"; echo "<a href='searchdocs.php'>Go Back</a>"; } } //mysql_close(); } } ?> PHP:
the code should be correct. It could be your query. do have have the exact keywords you are searching in the database field word? if there are not exact words than your query should use 'like' instead of '=' $aQuery = "select title, left(description, 100) as remedy from articles where articleid in (select articleids from searchwords where word like '%{$arrWords[$i]}%')"; Code (markup):
select title, left(description, 100) as remedy from articles where articleid in (select articleids from searchwords) use this query on your database and check if it returns any results
it still returns 1 result. the thing is it always returns the first value from the database & somehow i think there could be something wrong with the looping part
Do a comparison select articleid from articles select articleids from searchwords Do they have matching ids?
i don't think there's something wrong with the data, but i donno this is my database articles http://yfrog.com/euarticlesj searchwords http://yfrog.com/gisearchwordsj
I see...this should work <?php $submit = $_POST["submit"]; $keywords = $_POST["keywords"]; if(isset($submit) || isset($keywords)) { doSearch($keywords); } else { getKeywords(); } function getKeywords() { ?> <html> <head> <title> Enter Search Keywords </title> </head> <body bgcolor="#FFFFFF"> <form name="frmKW" action="index.php" method="post"> <h1>Keyword Search</h1> Enter keywords to search on: <input type="text" name="keywords" maxlength="100"> <br><br><input type="submit" name="submit" value="Search"> </form> </body> </html> <?php } function doSearch($search_keywords) { $arrWords = explode(" ", $search_keywords); if(sizeof($arrWords) == 0 || $search_keywords == "") { echo "You didn't enter any keywords<br>"; echo "<a href='searchdocs.php'>Go Back</a>"; } else { for($i = 0; $i < sizeof($arrWords); $i++) { //echo $arrWords[0]; $query = "select articleids from searchwords where word = '{$arrWords[$i]}'"; $result = mysql_query($query); if(mysql_num_rows($result) > 0) { // Get the id's of the articles $row = mysql_fetch_array($result); $arrIds = $row[0]; $aQuery = "select title, left(description, 100) as remedy from articles where articleid in ( $arrIds ) "; $aResult = mysql_query($aQuery); $count = 0; $articles = array(); if(mysql_num_rows($aResult) > 0) { while($aRow = mysql_fetch_array($aResult)) { $articles[$count] = array ( "articleid" => $aRow["articleid"], "title" => $aRow["title"], "remedy" => $aRow["remedy"] ); $count++; } } if(isset($articles)) { $articles = array_unique($articles); echo "<h1>" . sizeof($articles); echo (sizeof($articles) == 1 ? " article" : " articles"); echo " found:</h1>"; foreach($articles as $a => $value) { ?> <a href="article.php?articleId=<?php echo $articles[$a]["articleid"]; ?>"> <b><u><?php echo $articles[$a]["title"]; ?></u></b> </a> <br><?php echo $articles[$a]["remedy"] . "..." ; ?> <br> <a href="article.php?articleId=<?php echo $articles[$a]; ?>"> http://www.mysite.com/article.php?articleId=<?php echo $articles[$a]["articleid"]; ?> </a> <br><br> <?php } } else { echo "No results found for '$search_keywords'<br>"; echo "<a href='searchdocs.php'>Go Back</a>"; } } mysql_close(); } } } ?> Code (markup):
ya, sorry, i have changed it but it still returns 1 result http://yfrog.com/58keywordsearchj http://yfrog.com/7bresultfj