GOOD CODE , BAD CODE
Recently I created an International Dialling Code Database.
I used PHP 4.3.4 & Mysql 4.0.8 maxdebug.
The database is called idc (international diallling codes) has 5 tables with an associated file that can be displayed.
The fields are id primary key auto increment, c_n (country name) , c_c (country code) , n_p (national prefix & i_p (international prefix).
The file is an html file stored in my
wwwroot directory with the same name as c_n. I deleted the file extension to the html file. This saves the file as a microsoft file & enabled the database to find & display the result.
This db has lots of working code including search a mysql database , paginating a mysql database , displaying files , php hyperlink ids.
I need help with register_globals = off function though. Some is not working because the tutorials I learnt were written before the php.ini code
was register_globals = on. & I also wanted to mix search the database script & the paginating script so the page will list numbers on the bottom of the search page that correspond to the search query like on google.
searchidc.php a form that activates s.php a search page.
<form name="form" action="s.php" method="get">
<input type="text" name="q" />
<input type="submit" name="Submit" value="Search" />
</form>
This is s.php a search databse form I got from devshed.
With register_globals = off the searchform worked but the next & previous buttons didn't work. Please tell me how to get this to work With register_globals = off.
With register_globals turned = on the next/prev buttons works fine, 🙂
Here is the code for S.php.
<?php
// Get the search variable from URL
$var = @$_GET['q'] ;
$trimmed = trim($var); //trim whitespace from the stored variable
// rows to return
$limit=10;
// check for an empty string and display a message.
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","root",""); //(host, username, password)
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("idc") or die("Unable to select database"); //select which database we're using
// Build SQL Query
$query = "select * from idc where c_n like \"%$trimmed%\"
order by c_n"; // EDIT HERE and specify your table and field names for the SQL query
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, offer a google search as an alternative
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
// google
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
// begin to show results set
echo "Results";
$count = 1 + $s ;
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["c_n"];
$title1 = $row["c_c"];
$title2 = $row["i_p"];
$title3 = $row["n_p"];
echo "$count.) $title<br />" ;
echo "<TD>
<a href=\"view.php?id=".$row["id"]."\">To view page results please click here</a>", $_SERVER['PHP_SELF'];
echo('<p>Country Name<br />' . $row['c_n'] . '<br /><a href="' . $row['c_n'] . '" ?>Click here for City & Area Codes</a><br />Country Code<br />' . $row['c_c'] . '<br />National Prefix<br />' . $row['n_p']. '<br />International Prefix<br />' . $row['i_p']. '</p>');
$count++ ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"".$_SERVER['PHP_SELF']."?s=$prevs&q=$var\"><<
Prev 10</a>  ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo " <a href=\"".$_SERVER['PHP_SELF']."?s=$news&q=$var\">Next 10 >></a>";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";
?>
I would like to iclude a paginating code I got on phpfreaks
in this search that would enable the user to click on numbered links on the bottom of the page.
But I'm not sure how to adjust the code to the above feautured code in s.php search query page.
Working paginating code.
This is the code for pages.php
<?php
// Database Connection
$db = mysql_connect("localhost", "root", "");
mysql_select_db("idc",$db);
// If current page number, use it
// if not, set one!
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
// Define the number of results per page
$max_results = 10;
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
// Perform MySQL query on only the current page number's results
$sql = mysql_query("SELECT * FROM idc LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){
// Build your formatted results here.
echo('<p>Country Name<br />' . $row['c_n'] . '<br /><a href="' . $row['c_n'] . '" ?>Click here for City & Area Codes</a><br />Country Code<br />' . $row['c_c'] . '<br />National Prefix<br />' . $row['n_p']. '<br />International Prefix<br />' . $row['i_p']. '</p>');
}
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM idc"),0);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo "<center>Select a Page<br />";
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo "</center>";
?>
The update the database function & delete records page.
The script doesn't work with register_globals = off
function that a lot of proffesional e-commerce web hosts running PHP are using as default. I would appreciate any help with this problem.
This code will run on a local server with register_globals = on
The script will delete & update the database records.
idcedit.php
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("idc",$db);
if ($submit) {
// here if no ID then adding else we're editing
if ($id) {
$sql = "UPDATE idc SET c_n='$c_n',c_c='$c_c',i_p='$i_p',n_p='$n_p' WHERE id=$id";
}
else {
$sql = "INSERT INTO idc (c_n,c_c,i_p,n_p) VALUES ('$c_n','$c_c','$i_p','$n_p')";
}
// run SQL against the DB
$result = mysql_query($sql);
echo "Record updated/edited!<p>";
}
elseif ($delete) {
// delete a record
$sql = "DELETE FROM idc WHERE id=$id";
$result = mysql_query($sql);
echo "$sql Record deleted!<p>";
}
else {
// this part happens if we don't press submit
if (!$id) {
// print the list if there is not editing
$result = mysql_query("SELECT * FROM idc",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?id=%s\">%s %s</a> \n", $PHP_SELF, $myrow["id"], $myrow["c_n"], $myrow["c_c"]);
printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF, $myrow["id"]);
}
}
?>
<br />
ADD A RECORD TO THE DATABASE BY USING THE FORM BELOW
<form method="post" action="<?php echo $PHP_SELF?>">
<?php
if ($id) {
// editing so select a record
$sql = "SELECT * FROM idc WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$id = $myrow["id"];
$c_n = $myrow["c_n"];
$c_c = $myrow["c_c"];
$i_p = $myrow["i_p"];
$n_p = $myrow["n_p"];
// print the id for editing
?>
<input type=hidden name="id" value="<?php echo $id ?>">
<?php
}
?>
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td align="right">
Country Name:<input type="Text" name="c_n" value="<?php echo $c_n ?>"><br></td>
</tr>
<tr>
<td align="right">
Country Code:<input type="Text" name="c_c" value="<?php echo $c_c ?>"><br></td>
</tr>
<tr>
<td align="right">
International Prefix:<input type="Text" name="i_p" value="<?php echo $i_p ?>"><br></td>
</tr>
<tr>
<td align="right">
National Prefix:<input type="Text" name="n_p" value="<?php echo $n_p ?>"><br>
<input type="Submit" name="submit" value="Enter information">
</td>
</tr>
</table>
</form>
<?php
}
?>
I would appreciate any help with this code .Please tell me how to make this script work with php register_globals = off.