Displaying Results with PHP?

Discussion in 'PHP' started by cro91, Oct 17, 2011.

  1. #1
    I see tutorials for PHP everywhere but what I can't figure out how to get the data that I have gotten from the database to show on my page and not on a new page.

    Anyone have any suggestions on what I should be using to do this?

    Ex:

    I have a search page that when the search is submited it returns all the values from a database that match it. Currently it just changes pages to the php's page. I would like it to update the page that the search is done on though.
     
    cro91, Oct 17, 2011 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    You need to show us the code or it will be hard to determine the problem?
     
    MyVodaFone, Oct 17, 2011 IP
  3. cro91

    cro91 Peon

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Oh, I can show you what I what I currently have if you would like.

    I have been trying for a few days now how to figure out how to get my page to display search results on the same page that you search from but cant figure out how to do it. Thought maybe some one here could help as I am new and have no idea what I am really doing. The DIV I am trying to update on the HTML is called "results_bg".

    Right now I can get it to show "alert1" when the button is pushed but it does not look like its submitting the data to the php script.

    HTML
    
    <script type="text/javascript">
    function get(){
       alert("alert1");
           $.post('php/test.php', { Action: form.Action.value },
               function(output){
                  alert("alert2");
                  $('#results_bg').html(output).show();
                });
    }
    </script>
    
    <form method="GET" ACTION="php/do_search.php" name="form">
        <ul class="cat_col">
              <li><input type="checkbox" name="Action" value="1"> <a href="#Action">Action</a></li>
        </ul>
        <input type='button' onclick='get();' value='Query MySQL' />
    </form>
    
    Code (markup):
    This is just a small portion of the whole page, I can post the rest if needed.

    PHP
    
                            $sql = 'SELECT Anime_Name FROM Anime';
                if (isset($_GET['Action'])) 
                {
                    $where = ' WHERE Action=\'1\'';
                }
                if (isset($_GET['Adventure'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Adventure=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Adventure=\'1\'';
                    }
                }
                            $letter = '';
                $addquery = " ORDER BY Anime_Name";
                $query = mysql_query($sql.$where.$addquery);
                  $num_rows = mysql_num_rows($query);
                if($num_rows > 0) 
                {
                    while ($row = mysql_fetch_assoc($query)){ 
                        // only if first letter is different from stored letter 
                        if (strtoupper(substr($row['Anime_Name'],0,1)) != $letter) 
                        { 
                            // store new first letter 
                            $letter = strtoupper(substr($row['Anime_Name'],0,1)); 
                            // and print it 
                            echo '<a href="#'.$letter.'">'.$letter.'</a> <br />';
                        } 
                        // then print the name 
                        echo $row['Anime_Name'].'<br />'; 
                    } 
                    unset($letter);
                }
                else if($num_rows < 1)
                {
                    echo "Seems like your search returned no results. Try eliminating a genre.";
                }
    
    
    Code (markup):
     
    cro91, Oct 17, 2011 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    <form method="GET" ACTION="php/do_search.php" name="form">

    Tells the script to go to the page/url "php/do_search.php"

    Is this the same page you want the results printed out on.

    In other words, the script on that page, is the search script, the script needs to be on the page you want the results printed out on
     
    Last edited: Oct 17, 2011
    MyVodaFone, Oct 17, 2011 IP
  5. cro91

    cro91 Peon

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I feel like a retard, that makes so much sense. It didn't even cross my mind to put the script in the page. How would I get it to update a div with the results?
     
    cro91, Oct 17, 2011 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    Probable better if you show us the entire page and the content of do_search.php

    Someone will post back advise or an amended script explaining what they changed
     
    MyVodaFone, Oct 17, 2011 IP
  7. cro91

    cro91 Peon

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ok, the page is live if you want to see it here (http://redplanetanime.com/search.html)

    I am going to play around with putting the script in the HTML somewhere.

    Thanks for all the help to MyVodaFone.

    Here is the PHP script
    
    <?php
        ini_set ("display_errors", "1"); 
        error_reporting(E_ALL);
        
        $con = mysql_connect("localhost","[I]username[/I]","[I]password[/I]") or die("Connection error");
        mysql_select_db("[I]databasename[/I]")or die("database doesn't exist");
        
        $where = '';
        
        if (isset($_GET['submit'])) 
            {
                echo "Test Test";
                $sql = 'SELECT Anime_Name FROM Anime';
                if (isset($_GET['Action'])) 
                {
                    $where = ' WHERE Action=\'1\'';
                }
                if (isset($_GET['Adventure'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Adventure=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Adventure=\'1\'';
                    }
                }
                if (isset($_GET['Comedy'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Comedy=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Comedy=\'1\'';
                    }
                }
                if (isset($_GET['Demons'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Demons=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Demons=\'1\'';
                    }
                }
                if (isset($_GET['Drama'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Drama=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Drama=\'1\'';
                    }
                }
                if (isset($_GET['Ecchi'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Ecchi=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Ecchi=\'1\'';
                    }
                }
                if (isset($_GET['Fantasy'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Fantasy=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Fantasy=\'1\'';
                    }
                }
                if (isset($_GET['Harem'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Harem=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Harem=\'1\'';
                    }
                }
                if (isset($_GET['Horror'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Horror=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Horror=\'1\'';
                    }
                }
                if (isset($_GET['Magic'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Magic=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Magic=\'1\'';
                    }
                }
                if (isset($_GET['Martial_Arts'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Martial_Arts=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Martial_Arts=\'1\'';
                    }
                }
                if (isset($_GET['Mecha'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Mecha=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Mecha=\'1\'';
                    }
                }
                if (isset($_GET['Music'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Music=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Music=\'1\'';
                    }
                }
                if (isset($_GET['Mystery'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Mystery=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Mystery=\'1\'';
                    }
                }
                if (isset($_GET['Ninja'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Ninja=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Ninja=\'1\'';
                    }
                }
                if (isset($_GET['Parody'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Parody=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Parody=\'1\'';
                    }
                }
                if (isset($_GET['Psychological'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Psychological=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Psychological=\'1\'';
                    }
                }
                if (isset($_GET['Reverse_Harem'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Reverse_Harem=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Reverse_Harem=\'1\'';
                    }
                }
                if (isset($_GET['Romance'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Romance=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Romance=\'1\'';
                    }
                }
                if (isset($_GET['Samurai'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Samurai=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Samurai=\'1\'';
                    }
                }
                if (isset($_GET['School_Life'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND School_Life=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE School_Life=\'1\'';
                    }
                }
                if (isset($_GET['Science_Fiction'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Science_Fiction=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Science_Fiction=\'1\'';
                    }
                }
                if (isset($_GET['Seinen'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Seinen=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Seinen=\'1\'';
                    }
                }
                if (isset($_GET['Shoujo'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Shoujo=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Shoujo=\'1\'';
                    }
                }
                if (isset($_GET['Shounen'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Shounen=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Shounen=\'1\'';
                    }
                }if (isset($_GET['Slapstick'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Slapstick=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Slapstick=\'1\'';
                    }
                }
                if (isset($_GET['Slice_of_Life'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Slice_of_Life=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Slice_of_Life=\'1\'';
                    }
                }
                if (isset($_GET['Sports'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Sports=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Sports=\'1\'';
                    }
                }
                if (isset($_GET['Supernatural'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Supernatural=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Supernatural=\'1\'';
                    }
                }
                if (isset($_GET['Thriller'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Thriller=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Thriller=\'1\'';
                    }
                }
                if (isset($_GET['Tragedy'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Tragedy=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Tragedy=\'1\'';
                    }
                }
                if (isset($_GET['Vampire'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Vampire=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Vampire=\'1\'';
                    }
                }
                $letter = '';
                $addquery = " ORDER BY Anime_Name";
                $query = mysql_query($sql.$where.$addquery);
                  $num_rows = mysql_num_rows($query);
                if($num_rows > 0) 
                {
                   // This will put header letters at the top of the results
                    while ($row = mysql_fetch_assoc($query)){ 
                        // only if first letter is different from stored letter 
                        if (strtoupper(substr($row['Anime_Name'],0,1)) != $letter) 
                        { 
                            // store new first letter 
                            $letter = strtoupper(substr($row['Anime_Name'],0,1)); 
                            // and print it 
                            echo '<a href="#'.$letter.'">'.$letter.'</a> <br />';
                        } 
                        // then print the name 
                        echo $row['Anime_Name'].'<br />'; 
                    } 
                    unset($letter);
                }
                else if($num_rows < 1)
                {
                    echo "Seems like your search returned no results. Try eliminating a genre.";
                }
              }
    ?>
    
    Code (markup):
     
    cro91, Oct 17, 2011 IP
  8. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #8
    If your going to try that, change all instances of $_GET to $_POST and change the form to <form method="post" action="" name="form">
     
    MyVodaFone, Oct 17, 2011 IP
  9. cro91

    cro91 Peon

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hey, I have it on my page and displaying info where I want it but now its executing as soon as the page has loaded. How can I get it to wait until they hit the submit button?

    I still think it may be easier if I could figure out how to pass the data from my php script back to a div without having to include the whole PHP script on one page.
     
    Last edited: Oct 18, 2011
    cro91, Oct 18, 2011 IP
  10. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    #10
    Try using switch instead of a whole bunch of ifelses, it looks a helluva lot tidier/
     
    Einheijar, Oct 18, 2011 IP
  11. cro91

    cro91 Peon

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    From what I read if you do a switch statement it will go until it finds one correct and then it will do them ALL after that. How would you suggest doing it?

    Now my problem is validating checkboxes :/
     
    Last edited: Oct 18, 2011
    cro91, Oct 18, 2011 IP
  12. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    #12
    
    
    switch (true) {
    case (!empty($_GET['aaa'])):
    break;
    case (!empty($_GET['bbb'])):
    break;
    }
    
    
    PHP:
     
    Einheijar, Oct 18, 2011 IP
  13. cro91

    cro91 Peon

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Wouldn't that stop the whole script the first time it hits a break? The reason I have IF's is because they have the option to select more then one genre.
     
    cro91, Oct 18, 2011 IP
  14. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #14
    Up above you mentioned that the script was running as the page opened, you can try and hold back the functions of that script by wrapping the following around it:

    if (isset($var)) { // change $var to your search term variable?
    
    PASTE YOUR SEARCH SCRIPT HERE
    
    } // dont forget this <-- at the end
    PHP:
     
    MyVodaFone, Oct 18, 2011 IP
  15. uske

    uske Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    hello MyVodaFone, you seem to know PHP. I need to implement one simple PHP mod for my simple warehouse inventory script. It should be fairly simple task to do for someone who knows. Are you interested? If you are, please send me PM and we can discuss details and price.
     
    uske, Oct 18, 2011 IP
  16. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    #16
    Ah right, just remove the breaks then, it'll check for the next criteria
     
    Einheijar, Oct 18, 2011 IP