1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP code for displaying multiple results

Discussion in 'PHP' started by whateveritis, Apr 10, 2010.

  1. #1
    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 :confused:
     
    whateveritis, Apr 10, 2010 IP
  2. webria

    webria Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you will give print at each sql line, you will see why it get only one results against keywords you type.
     
    webria, Apr 12, 2010 IP
  3. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    
     $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
     
    javaongsan, Apr 12, 2010 IP
  4. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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:
     
    whateveritis, Apr 12, 2010 IP
  5. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #5
    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):
     
    javaongsan, Apr 12, 2010 IP
  6. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ^ 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:
     
    whateveritis, Apr 12, 2010 IP
  7. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #7
    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):
     
    javaongsan, Apr 13, 2010 IP
  8. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    it still returns 1 result :( could it be something wrong with my database instead?
     
    whateveritis, Apr 13, 2010 IP
  9. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #9
    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
     
    javaongsan, Apr 13, 2010 IP
  10. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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 :(
     
    whateveritis, Apr 13, 2010 IP
  11. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #11
    Do a comparison
    select articleid from articles
    select articleids from searchwords
    Do they have matching ids?
     
    javaongsan, Apr 13, 2010 IP
  12. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    sorry but how do i make comparison? just change into $aQuery?
     
    whateveritis, Apr 14, 2010 IP
  13. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #13
    do you have phpmyadmin? try to install one and use the gui to check your data
     
    javaongsan, Apr 14, 2010 IP
  14. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    in fact, i'm using XAMPP & i build my database using phpmyadmin but how do i do a comparison? :confused:
     
    whateveritis, Apr 14, 2010 IP
  15. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #15
    you can go to phpmyadmin and do a browse on the 2 tables and check the data
     
    javaongsan, Apr 14, 2010 IP
  16. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #16
    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
     
    whateveritis, Apr 14, 2010 IP
  17. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #17
    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):
     
    javaongsan, Apr 14, 2010 IP
  18. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #18
    when i click the 'Submit' button, it goes back to the home page :confused:
     
    whateveritis, Apr 14, 2010 IP
  19. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #19
    change the index.php in the code to searchdocs.php
     
    javaongsan, Apr 14, 2010 IP
  20. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #20
    ya, sorry, i have changed it but it still returns 1 result

    http://yfrog.com/58keywordsearchj

    http://yfrog.com/7bresultfj
     
    whateveritis, Apr 14, 2010 IP