PHP code for displaying multiple results

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

  1. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #21
    Try this
    
    <?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
        {
    	   for($i = 0; $i < sizeof($arrWords); $i++)
            {
                $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 articleid, title, left(description, 100) as remedy from articles where articleid in ( $arrIds ) group by  articleid, title, description";
    				
    					$aResult = mysql_query($aQuery);
    					
    					$count=mysql_num_rows($aResult) ;
    					if($count > 0)
    					{
    						echo "<h1>" .$count;
    						echo ($count == 1 ? " article" : " articles");
    						echo " found:</h1>";
    						
    						while($aRow = mysql_fetch_array($aResult))
    						{							
    							echo '<a href="article.php?articleId='.$aRow["articleid"].'"><b><u>'.$aRow["title"].'</u></b></a>';							
    							echo '<br> '.$aRow["remedy"].'....<br><a href="article.php?articleId='.$aRow["articleid"].'">';
    							echo 'http://www.mysite.com/article.php?articleId='.$aRow["articleid"].'</a><br><br>';
    						}
    					}
    					else
    					{
    						echo "No results found for '$search_keywords'<br>";
    						echo "<a href='searchdocs.php'>Go Back</a>";
    					}
                    }
               
                mysql_close();
            }
    	}
    }
    ?>
    
    Code (markup):

     
    javaongsan, Apr 15, 2010 IP
  2. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #22
    OMG it's working, thanx a lot :D

    Well, i have another request for help. like from the screen shot i gave previously, it gave result of 'fever' & once the user click on it, it will direct the user to a page that contains the information of 'fever'. The information is already provided in the database. How to display those information? Do you know how to do that?

    Sorry for troubling you.
     
    whateveritis, Apr 15, 2010 IP
  3. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #23
    is it in article.php? paste your code here.
     
    javaongsan, Apr 15, 2010 IP
  4. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #24
    well, the article.php is only a dummy page, because i don't know how to link and display the information from the database after the result is displayed :(
     
    whateveritis, Apr 15, 2010 IP
  5. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #25
    Please provide a sample of the database table you want to display
     
    javaongsan, Apr 15, 2010 IP
  6. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #26
    i show with screen shots & some explanation

    ok so firstly the user type in the any keywords like for the example "cold", then these results come out
    http://img532.imageshack.us/i/86046657.jpg/

    then for example the user click on "Fever", an information page will be showed
    http://img205.imageshack.us/i/14100289.jpg/

    or if the user click on like say "Cough"
    http://img338.imageshack.us/i/72793219.jpg/

    The "title", "description" & "remedy" are all from the database called "ailment" inside a table called "articles".

    Please let me know if you need more information.
     
    whateveritis, Apr 15, 2010 IP
  7. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #27
    This is how your article.php looks like
    
    <?
    $aQuery = "select articleid, title, description, left(description, 100) as remedy from articles where articleid =". $_GET['articleId'];
    $aResult = mysql_query($aQuery);
    
    while($aRow = mysql_fetch_array($aResult))
    {							
    	echo '<b><u>'.$aRow["title"].'</u></b>';	
    	echo '<br><b><u>Description :</u></b>'.$aRow["description"];
    	echo '<br><b><u>Remedy :</u></b>'.$aRow["remedy"];
    
    }
    mysql_close();
    ?>
    
    Code (markup):
     
    javaongsan, Apr 15, 2010 IP
  8. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #28
    i got this output
    http://img263.imageshack.us/img263/6083/outputp.jpg
     
    whateveritis, Apr 16, 2010 IP
  9. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #29
    check the double quotes and single quotes for the echo statement
     
    javaongsan, Apr 16, 2010 IP
  10. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #30
    it's still the same :confused:
     
    whateveritis, Apr 16, 2010 IP
  11. khawari

    khawari Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #31
    I want to start HTML and PHP, can anyone guide me from where to start ? any locations for institutes or internet educations!

    Thanks
     
    khawari, Apr 16, 2010 IP
  12. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #32
    i still have the problem to display the information & regarding this search function i wonder if it can search using multiple keywords for a specific result?

    can anyone help me with this please. Thanx in advance :)
     
    whateveritis, Apr 19, 2010 IP
  13. shmeeg

    shmeeg Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #33
    Try using this:

    <? $title=$_GET['title'];
    $result = mysql_query("SELECT * FROM articles WHERE title='$title'") or die(mysql_error());  
    while($rows=mysql_fetch_array($result)) { ?>
    
    <strong><? echo $rows['title']; ?></strong>
    <br /><br />
    <strong>Description: <? echo $rows['description']; ?>
    <br /><br />
    <strong>Remedy: <? echo $rows['remedy']; ?>
    PHP:
    This will work for all of the entries in the table "articles".
     
    shmeeg, Apr 20, 2010 IP
  14. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #34
    Hi, thanx for helping but the result only displays something like this:


    it didn't display the title, the description & the remedy :confused:
     
    whateveritis, Apr 20, 2010 IP
  15. shmeeg

    shmeeg Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #35
    Make sure you have the relevant database selected

    try adding this line at the top of the previous PHP code
    mysql_select_db("ailment") or die(mysql_error());
    PHP:
    let me know if it works
     
    shmeeg, Apr 21, 2010 IP
  16. whateveritis

    whateveritis Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #36
    i have already connect to the database & it displays what i have mention on the above post, the codes for your reference

    $con = mysql_connect("localhost","root","");
    		if (!$con)
    		  {
    		  die('Could not connect: ' . mysql_error());
    		  }
    
    		mysql_select_db("ailment", $con);
    
    $title=$_GET['title'];
    $result = mysql_query("SELECT * FROM articles WHERE title='$title'") or die(mysql_error());  
    while($rows=mysql_fetch_array($result)) { ?>
    
    <strong><? echo $rows['title']; ?></strong>
    <br /><br />
    <strong>Description: <? echo $rows['description']; ?>
    <br /><br />
    <strong>Remedy: <? echo $rows['remedy']; 
    PHP:
    i don't know where's the wrong part :(
     
    whateveritis, Apr 21, 2010 IP