Jump to content

Help trying to create a dynamic browse by page


justin7410
Go to solution Solved by mac_gyver,

Recommended Posts

Hey guys !

 

I am working on a browse page that will allow an end user to browse any name category they want , simply by going to one dynamic php page labeled as browse.php

 

This browse page will then check to see what is set after browse.php? and will then grab the appropriate data from the database and display this to the user. This  is my first attempt at doing such a task, and figured i was doing just fine, until the actual part of displaying new data per request.

 

my code is as follows:

$names_0_9= $_GET['names_0_9'];
$names_a  = $_GET['names_a'];
$names_b  = $_GET['names_b'];
$names_c  = $_GET['names_c'];
$names_d  = $_GET['names_d'];
$names_e  = $_GET['names_e'];
$names_f  = $_GET['names_f'];
$names_g  = $_GET['names_g'];
$names_h  = $_GET['names_h'];
$names_i  = $_GET['names_i'];
$names_j  = $_GET['names_j'];
$names_k  = $_GET['names_k'];
$names_l  = $_GET['names_l'];
$names_m  = $_GET['names_m'];
$names_n  = $_GET['names_n'];
$names_o  = $_GET['names_o'];
$names_p  = $_GET['names_p'];
$names_q  = $_GET['names_q'];
$names_r  = $_GET['names_r'];
$names_s  = $_GET['names_s'];
$names_t  = $_GET['names_t'];
$names_u  = $_GET['names_u'];
$names_v  = $_GET['names_v'];
$names_w  = $_GET['names_w'];
$names_x  = $_GET['names_x'];
$names_y  = $_GET['names_y'];
$names_z  = $_GET['names_z'];

if (isset($names_0_9) === true ) { 
echo 'names 0-9'; 
} else if (isset($names_a) === true ) { 
echo 'names a'; 
} else if (isset($names_ === true ) { 
echo 'names b'; 
}  else if (isset($names_c) === true ) { 
echo 'names c'; 
} else if (isset($names_d) === true ) {
 echo 'names d';
} else if (isset($names_e) === true ) {
 echo 'names e '; 
} else if (isset($names_f) === true ) {
 echo 'names f'; } 
else if (isset($names_g) === true ) {
 echo 'names g'; 
} else if (isset($names_h) === true ) {
 echo 'names h'; 
} else if (isset($names_i) === true ) {
 echo 'names i';
 } else if (isset($names_j) === true ) {
echo 'names j'; 
}else if (isset($names_k) === true ) { 
echo 'names k'; 
} else if (isset($names_l) === true ) { 
echo 'names l'; 
} else if (isset($names_m) === true ) { 
echo 'names m'; 
} else if (isset($names_n) === true ) {
 echo 'names n';
 } else if (isset($names_o) === true ) { 
echo 'names o'; 
} else if (isset($names_p) === true ) { 
echo 'names p'; 
} else if (isset($names_q) === true ) { 
echo 'names q';
 } else if (isset($names_r) === true ) { 
echo 'names r';
 } else if (isset($names_s) === true ) {
 echo 'shows s';
 } else if (isset($names_t) === true ) {
 echo 'names t'; 
} else if (isset($names_u) === true ) {
 echo 'names u';
 } else if (isset($names_v) === true ) { 
echo 'names v'; 
} else if (isset($names_w) === true ) { 
echo 'names w';
 } else if (isset($names_x) === true ) {
 echo 'names x';
 } else if (isset($names_y) === true ) { 
echo 'names y'; 
} else if (isset($names_z) === true ) {
 echo 'names z';
 } else {
header('Location: index.php');
exit();

 

 

All the links work correctly. Meaning, when i click on my link of a or link of b, i get sent to the browse.php?a or browse.php?b  with the correct message.

 

So i think to myself perfect, now i need to add the query for each letter and set the output for it to show to the user.

 

 

 

What i then did was tried to create a very general query that i knew would return with data from mysql for the letter A.

$query = "SELECT * FROM  `content` WHERE  `title` LIKE  'a%' LIMIT 0 , 30" 

Now this is where my novice php skills are being tested, and i lose course here to be able to correctly do what i want.

 

I place this query inside the  if statement ( or a similar query in the if else ) so that if this $_GET variable of 'a' is set that this query will be the main query to use.

 

end of my code becomes


else if (isset($names_a) === true ) { 
$query = "SELECT * FROM  `content` WHERE  `title` LIKE  'a%' LIMIT 0 , 30"
} else if (isset($names_y) === true ) {
echo 'names y';
} else if (isset($names_z) === true ) {
echo 'names z';
} else {
header('Location: index.php');
exit();
?>
<div class="left">
   
<div class="left_page_top">
<h1><a href="index.html">Home</a> >  Featured Movies</h1>
</div>
   


<div class="left_body">




<div class="movie_table">
        
<ul>




<?php


while ($row = mysql_fetch_assoc($query)) { 


extract($row); 






echo  '<li>';
echo    '<div class="profile_pic"><a href="profile.php?user=' .$profie_title . '&userid=' .$id .'"><img src="http://i.imgur.com/Epdwv1t.jpg" width="101" height="150"></a></div>';
echo   '<div class="user_about">';
echo        '<div class="user_about_text">';
echo       '<h1><a href="profile.php?user=' .$profile_title . '&userid=' .$id .'">' . $profile_title . ' (2013)</a></h1>';
echo       '<div class="c">Class: ' .$class . '</div>';
echo       'Join Date:'    . $join_date . '<br>';
echo       'Views: <span>194526</span> (<span>176</span> votes)<br>';
echo    'Votes:' .$votes .' <br>'; 
echo '</div>';
echo        '<div class="user_about_">';
echo      '<div class="vote">';
echo         '<div id="Mark">';
echo           '<div id="Maro">Rating:  <span id="Rate_36387"> ' . $user_rating . ' </span></div>';
echo         '</div>';
echo      '</div>';
echo      '</div>';
echo   '</div>';
echo '</li>';
}
?>
        
</ul>
</div>


   </div>




</div>
</div>

 

So what i thought i wanted to do since i am essentially outputting the members in a 2x 25 list that is scroll able depending on how much data is returned from the database.

 

now for some reason when i click on the letter a. nothing is outputted.

 

i really cant figure out where i went wrong or if i am even on the right track at this point.

 

any suggestions or any ideas ?

 

any help is much appreciated, i have been stuck on this for a couple days now. 

Edited by justin7410
Link to comment
Share on other sites

you are kind of missing the point of variables. you don't create a variable for each possible value, you create one variable, with a name that indicates the purpose of the data in the variable, that can hold any of the possible values. you are creating 27 times too much code (well at least 26.5 times too much because your 0-9 number range requires a little special handling to form a query that will match the range of leading numbers.)

 


 

your $_GET variable should be something like $_GET['choice'] or $_GET['category'] or $_GET['search']... all you would do is then validate that it contains an expected choice, then use it in one single query statement (not a hard-coded and separate query statement for each possible choice.)

 


 

as to why nothing was output when you tried your code, there's several possible reasons, but your code isn't readable as posted in the thread and until you have just the code you need with one variable and one query using that variable in it, not too may people are going to wade through your code.

Link to comment
Share on other sites

To me you would be better off pulling the content off the database by having a dynamic menu, an example:

 

Forgive the code, for it's old code that I have laying around and isn't the greatest. :happy-04:

    function php_navigation($nav_page, $page_set) {
        
        // Format for php_navigation
        // <ul>
        //    <li><a href="{link}">Name of Link</a></li>
        // </ul>
        
                
        
        $query = "SELECT id, category FROM pages ORDER BY new_blog_date ASC LIMIT 25";
        
        $output = "<ul>";
        if ($result = mysqli_query($db, $query)) {
            while ($page = mysqli_fetch_array($result)) {
                $output .= "<li><a href=\"" . $nav_page . "?page=" . urlencode($page["id"]) . "\">";
               
               if ($page_set == $page['id']) {
                      $output .= "<span class=\"selected\" >{$page["category"]}</span>";       
               } else {
                   $output .= "{$page["category"]}";
               }           
               
               $output .= "</a></li>";               
            }
            /* free result set */
            mysqli_free_result($result);            
        }
                
        $output .= "</ul>";        
        
        return $output;
    }   

This way the visitor could just click on a link and then by writing code (Hint a single if statement and a get statement (of course sanitized  :tease-01:  ) could pass the content to be displayed. This is what I would do instead of writing all that code, but I'm lazing and want to write as little code as possible. :tease-03:

   

 

Link to comment
Share on other sites

  • Solution

here's an example of just the right amount (not too much and not too little) of code you need to do this -

// make a list of the links (expected values)
$links = array_merge(array('0-9'),range('a','z'));

// produce the links
echo "Click link to search by: ";
foreach($links as $value){
    echo "<a href = '?search=$value'>".strtoupper($value)."</a> ";
}
echo "<br>";

// condition and validate the search value
$search = isset($_GET['search']) ? strtolower(trim($_GET['search'])) : '';
if(!in_array($search,$links)){
    // supplied search term is not an expected value
    $search = '';
}

// use the search value
if(empty($search)){
    // nothing selected
    echo "Please select something to search for.<br>";
} else {
    // perform the search
    echo "You searched for $search.<br>";
    if($search == '0-9'){
        // special handling for a 0-9 leading wild-card search
        $query = "SELECT * FROM  `content` WHERE  `title` REGEXP '^[0-9]' LIMIT 0 , 30";
    } else {
        $query = "SELECT * FROM  `content` WHERE  `title` LIKE  '$search%' LIMIT 0 , 30";
    }
    echo $query;

    // run the query, check for errors, check if any rows found ('No matching row(s)' message if none), display result
    
}
Link to comment
Share on other sites

Hey mac_gyver,

 

thanks for the extremely helpful post.

 

sorry for the late reply, it took me awhile to study your code and implement into what i was trying to do.

 

but i greatly appreciate this help, it made a world of difference , it not just helping me with the issue , but to better study what i was trying to do and apply a better technique.

 

thanks again man!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.