solvision Posted May 13, 2009 Share Posted May 13, 2009 Hey Guys After shrinking my website down considerably, i still have one more section i think can be shrunk further. I have 5 pages, all call the same database and display it the same way. The only thing that changes is the the value of a specific column in the where clause. I'm pretty sure these 5 pages could actually be 1 page, but not quite sure how. I will show the main page so you understand my meaning better: <div id="body_right"> <div id="content2"> <?php include ('library/tabs.php'); if(@strcmp($session->username,$req_user) == 0 || $session->isAdmin()) { // Call beginning of paging file $tbname = 'blablabla'; include ('library/paging1.php'); $sql = "SELECT blablabla_step1.*, blablabla_step2.* FROM blablabla_step1 LEFT JOIN blablabla_step2 ON blablabla_step1.al_uname = blablabla_step2.uname WHERE blablabla_step1.status = 'Send Offers' ORDER BY blablabla_step1.al_date Desc $limit"; $result = mysql_query($sql) or die('Error, list album failed. ' . mysql_error()); if (mysql_num_rows($result) == 0) { echo "No album yet"; } else { $total=mysql_num_rows($result); echo '<strong>' . $total . '</strong> result(s) found for the term: <strong>' . @$_POST['find'] . '</strong>'; echo '<table align="center" class="table_list">'; while ($row = mysql_fetch_assoc($result)) { include('library/database/contactview.php'); } echo '</table>'; } // Call end of paging file include ('library/paging2.php'); } else{ include ('library/not_logged_in.php'); } ?> </div> </div> So the red highlighted where statement is the only thing that changes, here is another example of one of the others below: WHERE blablabla_step1.status != 'Send Offers' AND blablabla_step1.status != 'Not Available' AND blablabla_step1.status != 'Fee Received' AND blablabla_step1.status != 'Awaiting Docs' AND blablabla_step1.status != 'Active' Any ideas how to combine these five pages? And can that where statement actually be written shorter as well? i have tried using ( ) but cannot quite figured it out. Quote Link to comment https://forums.phpfreaks.com/topic/157929-make-5-pages-into-just-1/ Share on other sites More sharing options...
JonnoTheDev Posted May 13, 2009 Share Posted May 13, 2009 Simple - use URL parameters. The paremeter is used in the WHERE condition after it has been validated: http://www.xyz.com/page.php?param=abc http://www.xyz.com/page.php?param=def <?php // page.php $result = mysql_query("SELECT * FROM table WHERE field='".mysql_real_escape_string($_GET['param'])."'"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/157929-make-5-pages-into-just-1/#findComment-833143 Share on other sites More sharing options...
Dathremar Posted May 13, 2009 Share Posted May 13, 2009 Do what the OP said but plz dont put the $_GET variable directly into the sql Take the content of the $_GET variable and then check if contains the information that You want and then put it in the sql. Or even better just put some URL variable to determine what where statement to use for ex: 1 means "where bla = bla1" 2 means "where bla = bla2" etc So make a switch statement to determine which of the predefined sql to use. This is only for your own safty. Putting URl variables or anykind of variables directly into the sql can result sql injections. Hope I helped You P.S. I wrote this in a hurry so if u need more information please say so Quote Link to comment https://forums.phpfreaks.com/topic/157929-make-5-pages-into-just-1/#findComment-833152 Share on other sites More sharing options...
radi8 Posted May 13, 2009 Share Posted May 13, 2009 I also do this but to keep values OUT of the URL (I hate using the URL to pass data), I use $_SESSION variables to pass the data. This is just my preference and if you are not using session variables, it may not be feasible or will take some code restructuring to make work. Quote Link to comment https://forums.phpfreaks.com/topic/157929-make-5-pages-into-just-1/#findComment-833187 Share on other sites More sharing options...
JonnoTheDev Posted May 13, 2009 Share Posted May 13, 2009 Using a session is not possible in this case because you would never know the value to store in the session. That is why it is passed in the URL. Hence multiple parameters for 1 page. URL parameters are an essential part of web programming - used correctly and properly validated there are no issues. Quote Link to comment https://forums.phpfreaks.com/topic/157929-make-5-pages-into-just-1/#findComment-833196 Share on other sites More sharing options...
solvision Posted May 14, 2009 Author Share Posted May 14, 2009 Thanks guys, i figured it would be through passing the required where value. This has been really helpful. Now if i could only figure out my paging problem i'd be done Quote Link to comment https://forums.phpfreaks.com/topic/157929-make-5-pages-into-just-1/#findComment-833758 Share on other sites More sharing options...
Dathremar Posted May 14, 2009 Share Posted May 14, 2009 For paging u need couple of things to keep in mind. Like number of all rows, rows presented on page. So when you execute the query set up a limit in the SQL. And transfer from 1 page to another the number of rows that needs to be displayed (if it is changable) and the last number of the last displayed row. Also You can find some really nice paging script on the web. Just use google a bit have fun Quote Link to comment https://forums.phpfreaks.com/topic/157929-make-5-pages-into-just-1/#findComment-833973 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.