Jump to content

NEWBIE Q: PHP and ?mode=... link/IDs/upgrade search function


osti

Recommended Posts

Hi.

I hope this is the right Thread for this question.

 

I'm trying to make a address book, and one of the features is giving me some problems. I put together a search script (search.php) from an online tutorial, but now I can't:

 

- make a link to the form file (contact.php) in edit mode (?mode=edit), delete mode (?mode=delete);

- when the link in mode is pressed (edit or delete) the contact id ($id) from mysql base should be used to pre-write the contact's data in the form;

- echo a message if no words were written in input.

 

I'm still new at PHP coding, but I do understand enough of the php to know I won't be able to make this on my own.

I use WindowsXPPro, Apache 2.2, PHP5, MySQL5.

 

Here are my scripts, what I have put together so far:

 

- search.php:

<?php

include('db_login.php');



$connection = mysql_connect($db_host, $db_username, $db_password);

if (!$connection) {

        die ("Could not connect to the database: <br />". mysql_error());

}



// izbira baze

$db_select = mysql_select_db($db_database);

if (!$db_select){

        die ("Could not select the database: <br />". mysql_error());

}



$rowsPerPage = 5;

$pageNum = 1;



if(isset($_GET['page'])) {

    $pageNum = $_GET['page'];

}



if(isset($_GET['search'])) {

    $search = $_GET['search'];



/*if(!$search) {

    echo "No search entered, please go back and fill in the fields properly.";

}*/

}



// counting the offset

$offset = ($pageNum - 1) * $rowsPerPage;



$keywords = explode(" ", $search);



$query = "SELECT id,f_name,l_name,phone FROM stiki " . "WHERE l_name LIKE '%".$keywords['0']."%'";



for ($i=1; $i<count($keywords); $i++) {

    $query = $query." OR l_name LIKE '%".$keywords[$i]."%'";

}



$query = $query." ORDER BY l_name ASC LIMIT $offset, $rowsPerPage";



$result2 = mysql_query($query) or die(mysql_error());

?>

    <table align="center" bgcolor="#E1E5E7" width="800" border="1" cellspacing="0" cellpadding="0" >

          <tr>

            <td width="152" height="76"><img src="images/CDELogo1.gif"></td>

            <td align="left" valign="bottom"><font color="#8A9297" size="+3"> phonebook</font></td>

          </tr>

          <tr>

            <td valign="top">

                <table width="152" border="0" cellspacing="0" cellpadding="0">

                      <tr>

                        <td><font color="#8A9297" size="+1"> Menu</font></td>

                      </tr>

                      <tr>

                        <td><font color="#8A9297" size="+1"> <a href="contact.php">Add</a></font></td>

                      </tr>

                      <tr>

                        <td><font color="#8A9297" size="+1"> <a href="search.php">Search/Edit</a></font></td>

                      </tr>

                      <tr>

                        <td> </td>

                      </tr>

                </table>

            </td>

            <td>

            <form method="GET" action="search_limited.php">

            <b>Search:</b>

            <input name="search" type="text" value="<?php echo $_GET['search']; ?>" size="20">

            <input type="submit" value="Search">

            </form>

                <table width="100%" style="border:1px solid #000000;">

<?php

$keywords = explode(" ", $search);



$query = "SELECT COUNT(id) AS numrows FROM stiki " . "WHERE l_name LIKE '%".$keywords['0']."%'";



for ($i=1; $i<count($keywords); $i++) {

    $query = $query." OR l_name LIKE '%".$keywords[$i]."%'";

    }



$result = mysql_query($query) or die('Error, query failed');

$row = mysql_fetch_array($result, MYSQL_ASSOC);

$numrows = $row['numrows'];



// how many pages we have when using paging?

$maxPage = ceil($numrows/$rowsPerPage);



// print the link to access each page

$self = $_SERVER['PHP_SELF'];

$nav = '';



for($page = 1; $page <= $maxPage; $page++) {

        if ($page == $pageNum)

    {

        $nav .= " $page ";

    }

else

    {

        $nav .= " <a href=\"$self?page=$page&search=$search\">$page</a> ";

    }

}



// naprej, nazaj, prva, zadnja stran



if ($pageNum > 1) {



    $page = $pageNum - 1;

    $prev = " <a href=\"$self?page=$page&search=$search\">[Prev]</a> ";



    $first = " <a href=\"$self?page=1&search=$search\">[First Page]</a> ";

}

else {

    $prev = ' ';

    $first = ' ';

}



if ($pageNum < $maxPage) {

    $page = $pageNum + 1;

    $next = " <a href=\"$self?page=$page&search=$search\">[Next]</a> ";



    $last = " <a href=\"$self?page=$maxPage&search=$search\">[Last Page]</a> ";

}

else {

    $next = ' ';

    $last = ' ';

}



while($row = mysql_fetch_array($result2)) {



    echo "<tr>";

    echo "<td style='border-bottom: 1px solid #000000;'>";

    echo "<tr><td><a href=\"contact.php?mode=edit&id=$_POST[sel_id]\">".$row['f_name']."</a></td>" . "<td>".$row['l_name']."</td>" . "<td>".$row['phone']."</td></tr>";

    echo "</td>";

    echo "</tr>";

}



    // povezave za navigacijo

    echo "<tr>";

    echo "<td>";

    echo "<center>".$first . $prev . $nav . $next . $last."</center>";

    echo "</td>";

    echo "</tr>";



?>

                </table>

            </td>

        </tr>

    </td>

    </td>

  </tr>

</table>

 

- contact.php:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />

<title>Contact</title>

</head>



<body>



<?php



if ( mode=="add")

{

echo "<table align=\"center\" bgcolor=\"#E1E5E7\" width=\"800\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" >

  <tr>

    <td width=\"152\" height=\"76\"><img src=\"images/CDELogo1.gif\"></td>

    <td align=\"left\" valign=\"bottom\"><font color=\"#8A9297\" size=\"+3\"> phonebook</font></td>

  </tr>

  <tr>

    <td valign=\"top\">

        <table width=\"152\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">

          <tr>

            <td><font color=\"#8A9297\" size=\"+1\"> Menu</font></td>

          </tr>

          <tr>

            <td><font color=\"#8A9297\" size=\"+1\"> <a href=\"contact.php\"?mode=add>Add</a></font></td>

          </tr>

          <tr>

            <td><font color=\"#8A9297\" size=\"+1\"> <a href=\"search.php\">Search/Edit</a></font></td>

          </tr>

          <tr>

            <td> </td>

          </tr>

        </table>

    </td>

    <td>

        <form method=\"post\" action=\"contact.php\"?mode=add>

        <table width=\"648\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">

          <tr>

                 <td width=\"90\"> </td>

               <td width=\"558\"><font color=\"#8A9297\" size=\"+2\">Add</font></td>

          </tr>

              <tr>

                <td> First Name</td>

                <td><input type=\"text\" id=\"f_name\" name=\"f_name\" size=\"30\"></td>

              </tr>

              <tr>

                <td> Last Name</td>

                <td><input type=\"text\" id=\"l_name\" name=\"l_name\" size=\"45\"></td>

              </tr>

              <tr>

                <td> Phone</td>

                <td><input type=\"text\" id=\"phone\" name=\"phone\" size=\"50\"></td>

              </tr>

              <tr>

                <td> </td>

                <td align=\"right\"><input type=\"submit\" name=\"Submit\" value=\"Submit\"><input type=\"reset\" name=\"Reset\" value=\"Reset\">            

                </td>

              </tr>

        </table>

        </form>

    </td>

  </tr>

</table>

";

}



if ( mode =="edit")

{

echo "<table align=\"center\" bgcolor=\"#E1E5E7\" width=\"800\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" >

  <tr>

    <td width=\"152\" height=\"76\"><img src=\"images/CDELogo1.gif\"></td>

    <td align=\"left\" valign=\"bottom\"><font color=\"#8A9297\" size=\"+3\"> phonebook</font></td>

  </tr>

  <tr>

    <td valign=\"top\">

        <table width=\"152\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">

          <tr>

            <td><font color=\"#8A9297\" size=\"+1\"> Menu</font></td>

          </tr>

          <tr>

            <td><font color=\"#8A9297\" size=\"+1\"> <a href=\"contact.php?mode=add\">Add</a></font></td>

          </tr>

          <tr>

            <td><font color=\"#8A9297\" size=\"+1\"> <a href=\"search.php\">Search/Edit</a></font></td>

          </tr>

          <tr>

            <td> </td>

          </tr>

        </table>

    </td>

    <td>

        <form method=\"post\" action=\"p_contact.php\">

        <table width=\"648\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">

          <tr>

                 <td width=\"90\"> </td>

               <td width=\"558\"><font color=\"#8A9297\" size=\"+2\">Edit</font></td>

          </tr>

              <tr>

                <td> First Name</td>

                <td><input type=\"text\" id=\"f_name\" name=\"f_name\" size=\"30\"></td>

              </tr>

              <tr>

                <td> Last Name</td>

                <td><input type=\"text\" id=\"l_name\" name=\"l_name\" size=\"45\"></td>

              </tr>

              <tr>

                <td> Phone</td>

                <td><input type=\"text\" id=\"phone\" name=\"phone\" size=\"50\"></td>

              </tr>

              <tr>

                <td> </td>

                <td align=\"right\"><input type=\"submit\" name=\"Submit\" value=\"Submit\"><input type=\"reset\" name=\"Reset\" value=\"Reset\">            

                </td>

              </tr>

        </table>

        </form>

    </td>

  </tr>

</table>

";

}



?>



</body>

</html>

Thx, Full-Demon, it helped. Do you know how to resolve the other two problems? At the moment I'm trying to solve the ID problem by:

 

- adding type="hidden" name="id" input field in the Search form (in search.php);

- adding :a href=\"contact.php?mode=edit&id=\" title=\"Edit\": as a link in search.php;

- adding if ( $_GET['mode'] == "edit" && $_POST['id'] == "") in contact.php;

 

but saddly it ain't working. Any clues?

 

LP

osti

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.