CodeMama Posted August 3, 2009 Share Posted August 3, 2009 I want this to not show any results unless one is picked..it is defaulting I fixed this the other day with using $_server(script_file) but all of a sudden that won't work with the rewritten (by boss) script.... HELP <?php //alphabetical pagination links //if the page is browse default to A's if on other pages just show pagination links and not default listings //Create array with letters AND number sign $letters = range('A','Z'); array_push($letters, 'nums'); $menu = ''; $selectedLetter = isset($_GET['letter']) ? $_GET['letter'] : ""; foreach($letters as $letter) { if($letter == $selectedLetter && $selectedLetter != 'nums') { $menu .= sprintf('%s ', $letter); } else if($letter == $selectedLetter && $selectedLetter == 'nums') { $menu .= sprintf('%s ', '#'); } else { if($letter == 'nums') { $menu .= sprintf('<a href="browseMatt.php?letter=%s">%s</a> ', 'nums', '#'); } else { $menu .= sprintf('<a href="browseMatt.php?letter=%s">%s</a> ', $letter, $letter); } } } echo "<div align=\"center\"><b>{$menu}</b><br /></div>"; //Show all restaurants that start with $letter not between "A" and "Z" if ($selectedLetter == "nums") { for($i = 0; $i <= 9; $i++) { $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '$i%'"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { $name = $row['name']; printf('<a href="view.php?ID=%s"><b>%s</b><br />%s<br /><br /></a>', $row['ID'], $row['name'], $row['address']); } } } else { $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '$selectedLetter%'"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { $name = $row['name']; printf('<a href="view.php?ID=%s"><b>%s</b><br />%s<br /><br /></a>', $row['ID'], $row['name'], $row['address']); } } ?> Link to comment https://forums.phpfreaks.com/topic/168680-solved-setting-default-view-to-nothingnot-working/ Share on other sites More sharing options...
TeNDoLLA Posted August 3, 2009 Share Posted August 3, 2009 This is what you have now.. (shortened your code a lil but you get the idea of the structure) <?php if (letter == num) { // get numeric results } else { // get by letter.. and this is executed always when numeric is not.. // no matter did anyone choose a letter or not. } So you need to add one else if more if you dont want to show anythign if letter is not picked. Something like this... <?php if ($letter == 'nums') { // get numeric results } else if (strlen($letter)) { // get by letter.. } // otherwise dont do anything.. Also there is no point making multiple mystl queries inside the for loop when getting the results for all numbers, just create the query outside the loop and query just once to get all. <?php // will generate the desired query for you.. (not tested) $where = 'WHERE '; for ($i=0; $i<10; $i++) { $where .= 'name LIKE \'' . $i . '%\' OR '; } $where = substr($where, 0, strlen($where)-4); $sql = "SELECT DISTINCT ID, name, address FROM restaurants $where"; Link to comment https://forums.phpfreaks.com/topic/168680-solved-setting-default-view-to-nothingnot-working/#findComment-889870 Share on other sites More sharing options...
CodeMama Posted August 3, 2009 Author Share Posted August 3, 2009 I only want it to show nothing if it's not on the browse.php page, like if it's on test.php it needs to show nothing just the links, if it's on browse.php then it should default and show the A listings. Link to comment https://forums.phpfreaks.com/topic/168680-solved-setting-default-view-to-nothingnot-working/#findComment-889894 Share on other sites More sharing options...
TeNDoLLA Posted August 3, 2009 Share Posted August 3, 2009 So I assume the code you posted was that browse.php then? If so.. just change tihs one line: <?php $selectedLetter = isset($_GET['letter']) ? $_GET['letter'] : ""; to this.. <?php $selectedLetter = isset($_GET['letter']) ? $_GET['letter'] : "A"; Link to comment https://forums.phpfreaks.com/topic/168680-solved-setting-default-view-to-nothingnot-working/#findComment-889897 Share on other sites More sharing options...
darkfreaks Posted August 3, 2009 Share Posted August 3, 2009 no she wants to make it so if its on one page it echoes something else nothing <?php $path= $_SERVER['PHP_SELF']; //current path basename($path); //sets path to page.php if($path=="browse.php") { //code }else{ //code } ?> Link to comment https://forums.phpfreaks.com/topic/168680-solved-setting-default-view-to-nothingnot-working/#findComment-889912 Share on other sites More sharing options...
TeNDoLLA Posted August 3, 2009 Share Posted August 3, 2009 Ah I see... btw don't use PHP_SELF, use basename($_SERVER['SCRIPT_NAME']) instead, because PHP_SELF is vulnerable to XSS attacks and practically can be manipulated by users as much as they ever want unless properly sanitized. Link to comment https://forums.phpfreaks.com/topic/168680-solved-setting-default-view-to-nothingnot-working/#findComment-889941 Share on other sites More sharing options...
CodeMama Posted August 3, 2009 Author Share Posted August 3, 2009 STill won't work...argh.... here is script now again: //Create array with letters AND number sign $letters = range('A','Z'); array_push($letters, 'nums'); $menu = ''; $selectedLetter = isset($_GET['letter']) ? $_GET['letter'] :NULL; //if the page is browse default to A's if on other pages just show pagination links and not default listings if ("browse.php" == $_SERVER['SCRIPT_FILENAME']) { $letter = "A"; } else { $letter = NULL; } foreach($letters as $letter) { if($letter == $selectedLetter && $selectedLetter != 'nums') { $menu .= sprintf('%s ', $letter); } else if($letter == $selectedLetter && $selectedLetter == 'nums') { $menu .= sprintf('%s ', '#'); } else { if($letter == 'nums') { $menu .= sprintf('<a href="browseMatt.php?letter=%s">%s</a> ', 'nums', '#'); } else { $menu .= sprintf('<a href="browseMatt.php?letter=%s">%s</a> ', $letter, $letter); } } } echo "<div align=\"center\"><b>{$menu}</b><br /></div>"; //Show all restaurants that start with $letter not between "A" and "Z" if ($selectedLetter == "nums") { for($i = 0; $i <= 9; $i++) { $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '$i%'"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { $name = $row['name']; printf('<a href="view.php?ID=%s"><b>%s</b><br />%s<br /><br /></a>', $row['ID'], $row['name'], $row['address']); } } } else { $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '$selectedLetter%'"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { $name = $row['name']; printf('<a href="view.php?ID=%s"><b>%s</b><br />%s<br /><br /></a>', $row['ID'], $row['name'], $row['address']); } } Link to comment https://forums.phpfreaks.com/topic/168680-solved-setting-default-view-to-nothingnot-working/#findComment-889950 Share on other sites More sharing options...
TeNDoLLA Posted August 3, 2009 Share Posted August 3, 2009 This is not the best way you could do it, but if your application will be small it will do just fine I think. I did not test it or anything so there might be some errors. But maybe you can try it out. <?php // Function to create paging. function createPaging($selectedLetter = null) { $letters = range('A','Z'); array_push($letters, 'nums'); $menu = ''; foreach($letters as $letter) { if($letter == $selectedLetter && $selectedLetter != 'nums') { $menu .= sprintf('%s ', $letter); } else if($letter == $selectedLetter && $selectedLetter == 'nums') { $menu .= sprintf('%s ', '#'); } else { if($letter == 'nums') { $menu .= sprintf('<a href="browseMatt.php?letter=%s">%s</a> ', 'nums', '#'); } else { $menu .= sprintf('<a href="browseMatt.php?letter=%s">%s</a> ', $letter, $letter); } } } return $menu; } // Function to show just paging. function index() { // Echo only paging. echo createPaging(); } // Function to show results if page was given. function browseMatt($selectedLetter) { // Echo paging. echo createPaging($selectedLetter); //Show all restaurants that start with $letter not between "A" and "Z" if ($selectedLetter == "nums") { for($i = 0; $i <= 9; $i++) { $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '$i%'"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { $name = $row['name']; printf('<a href="view.php?ID=%s"><b>%s</b><br />%s<br /><br /></a>', $row['ID'], $row['name'], $row['address']); } } } else { $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '$selectedLetter%'"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { $name = $row['name']; printf('<a href="view.php?ID=%s"><b>%s</b><br />%s<br /><br /></a>', $row['ID'], $row['name'], $row['address']); } } } // Main controller which page to show. if (isset($_GET['letter'])) { browseMatt($_GET['letter']); } else { index(); } Link to comment https://forums.phpfreaks.com/topic/168680-solved-setting-default-view-to-nothingnot-working/#findComment-890000 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.