7 Replies Latest reply: Sep 21, 2014 1:14 PM by Rob Hecker2 RSS

    Jump to newly created record

    Max Resnikoff Community Member

      Hi,

       

      I know how to create jump links, but with normal text and anchors.

       

      But how would I jumpto a newly created record?

       

      For example:

      1. The user fills in the form
      2. They are redirected to the record page
      3. The page jumps to where the record they just created is. (Maybe even highlight it light green to indicate that is their newly created record).

       

       

      Thank you!

        • 1. Re: Jump to newly created record
          bregent CommunityMVP

          You just need to use the primary key of the record in the SQL WHERE clause. If you are using an autoincrement field to generate the key, then use mysql_insert_id(), or the mysqli or pdo equivalent.

          PHP: mysql_insert_id - Manual

          • 2. Re: Jump to newly created record
            Max Resnikoff Community Member

            sorry i dont really understand the tutorial.

             

            I know how to send the ID to the next page, but how do i make the page jump to the newly created user in the record list?

            • 3. Re: Jump to newly created record
              Rob Hecker2 CommunityMVP

              So you are saying that you have a long list of records, and you want all those records to show, but you want the newly created record to stand out as green, and be visible (in case the full list is so long that not all records are visible)?

               

              Then add something like the following to the code that builds the set of records, and make sure #anchor is referenced in the url.

               

              if ($_GET['id'] == $result['id']){

              echo "<a name='anchor'></a><p style='color:green'>$record</p>";

              } else {

              echo "<p>$record</p>";

              }

              • 4. Re: Jump to newly created record
                Max Resnikoff Community Member

                what do i put instead of $record?

                • 5. Re: Jump to newly created record
                  Rob Hecker2 CommunityMVP

                  what do i put instead of $record?

                   

                  Whatever data you are pulling from your database, in your while loop, something like the following

                   

                  while($result = $sql->fetch(PDO::FETCH_ASSOC)){  

                  extract ($result);

                   

                  if ($_GET['id'] == $result['id']){

                  echo "<a name='anchor'></a><p style='color:green'>$record</p>";

                  } else {

                  echo "<p>$name $address $whatever</p>";

                  }}

                  • 6. Re: Jump to newly created record
                    Max Resnikoff Community Member

                    So sorry, but i still don't understand. I'm new to this!

                     

                    Here is my code for the record page:

                     

                    <?php require_once('Connections/drama_database.php'); ?>

                    <?php

                    if (!isset($_SESSION)) {

                      session_start();

                    }

                    $MM_authorizedUsers = "";

                    $MM_donotCheckaccess = "true";

                     

                    // *** Restrict Access To Page: Grant or deny access to this page

                    function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {

                      // For security, start by assuming the visitor is NOT authorized.

                      $isValid = False;

                     

                      // When a visitor has logged into this site, the Session variable MM_Username set equal to their username.

                      // Therefore, we know that a user is NOT logged in if that Session variable is blank.

                      if (!empty($UserName)) {

                        // Besides being logged in, you may restrict access to only certain users based on an ID established when they login.

                        // Parse the strings into arrays.

                        $arrUsers = Explode(",", $strUsers);

                        $arrGroups = Explode(",", $strGroups);

                        if (in_array($UserName, $arrUsers)) {

                          $isValid = true;

                        }

                        // Or, you may restrict access to only certain users based on their username.

                        if (in_array($UserGroup, $arrGroups)) {

                          $isValid = true;

                        }

                        if (($strUsers == "") && true) {

                          $isValid = true;

                        }

                      }

                      return $isValid;

                    }

                     

                    $MM_restrictGoTo = "login.php";

                    if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) { 

                      $MM_qsChar = "?";

                      $MM_referrer = $_SERVER['PHP_SELF'];

                      if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";

                      if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0)

                      $MM_referrer .= "?" . $_SERVER['QUERY_STRING'];

                      $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);

                      header("Location: ". $MM_restrictGoTo);

                      exit;

                    }

                    ?>

                     

                    <title>Equipment Inventory</title>

                    <?php error_reporting(0);

                    ini_set('display_errors', 0);

                    ?>

                    <?php session_start();?>

                    <!--HEADER CONTENT START -->

                    <link href="stylesheet.css" rel="stylesheet" type="text/css" />

                    <head>

                    <style type="text/css">

                    body{ margin:0px; background:#FFF;}

                    .header {

                        height:165px;

                        background:#FFF;

                        border:1px solid#CCC;

                        position:fixed;

                        width:100%;

                        top:0px;

                    }

                    </style>

                    </head>

                     

                    <div class="header">

                    <?php include('defaultheader.php');?>

                    </div>

                    <div class="addnewequipsidebutton"></div>

                    <br>

                    <br>

                    <br>

                    <br>

                    <br>

                    <br>

                    <br>

                    <br>

                    <br>

                    <br>

                     

                    <!--HEADER CONTENT END -->

                     

                    <?php

                    if ($_GET['id'] == $result['id']){

                    echo "<a href='#jump'></a><p style='color:green'>$record</p>";

                    } else {

                    echo "<p>$record</p>";

                    }

                    ?>

                     

                      <?php

                    if (!function_exists("GetSQLValueString")) {

                    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

                    {

                      if (PHP_VERSION < 6) {

                        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

                      }

                     

                      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

                     

                      switch ($theType) {

                        case "text":

                          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

                          break;  

                        case "long":

                        case "int":

                          $theValue = ($theValue != "") ? intval($theValue) : "NULL";

                          break;

                        case "double":

                          $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

                          break;

                        case "date":

                          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

                          break;

                        case "defined":

                          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

                          break;

                      }

                      return $theValue;

                    }

                    }

                     

                     

                    $currentPage = $_SERVER["PHP_SELF"];

                     

                    if ((isset($_POST['id'])) && ($_POST['id'] != "")) {

                      $deleteSQL = sprintf("DELETE FROM equipment WHERE id=%s",

                                           GetSQLValueString($_POST['id'], "int"));

                     

                      mysql_select_db($database_drama_database, $drama_database);

                      $Result1 = mysql_query($deleteSQL, $drama_database) or die(mysql_error());

                     

                      $deleteGoTo = "equipmentinventory.php";

                      if (isset($_SERVER['QUERY_STRING'])) {

                        $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";

                        $deleteGoTo .= $_SERVER['QUERY_STRING'];

                      }

                      header(sprintf("Location: %s", $deleteGoTo));

                    }

                     

                    $maxRows_invlist = 10;

                    $pageNum_invlist = 0;

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

                      $pageNum_invlist = $_GET['pageNum_invlist'];

                    }

                    $startRow_invlist = $pageNum_invlist * $maxRows_invlist;

                     

                    $colname_invlist = "-1";

                    if (isset($_POST['txt_search'])) {

                      $colname_invlist = $_POST['txt_search'];

                    }

                    mysql_select_db($database_drama_database, $drama_database);

                    $query_invlist = sprintf("SELECT * FROM equipment WHERE name = %s", GetSQLValueString($colname_invlist, "text"));

                    $query_limit_invlist = sprintf("%s LIMIT %d, %d", $query_invlist, $startRow_invlist, $maxRows_invlist);

                    $invlist = mysql_query($query_limit_invlist, $drama_database) or die(mysql_error());

                    $row_invlist = mysql_fetch_assoc($invlist);

                     

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

                      $totalRows_invlist = $_GET['totalRows_invlist'];

                    } else {

                      $all_invlist = mysql_query($query_invlist);

                      $totalRows_invlist = mysql_num_rows($all_invlist);

                    }

                    $totalPages_invlist = ceil($totalRows_invlist/$maxRows_invlist)-1;

                     

                    $maxRows_invlist = 10;

                    $pageNum_invlist = 0;

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

                      $pageNum_invlist = $_GET['pageNum_invlist'];

                    }

                    $startRow_invlist = $pageNum_invlist * $maxRows_invlist;

                     

                     

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

                      $totalRows_invlist = $_GET['totalRows_invlist'];

                    } else {

                      $all_invlist = mysql_query($query_invlist);

                      $totalRows_invlist = mysql_num_rows($all_invlist);

                    }

                    $totalPages_invlist = ceil($totalRows_invlist/$maxRows_invlist)-1;

                     

                    $maxRows_invlist = 20;

                    $pageNum_invlist = 0;

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

                      $pageNum_invlist = $_GET['pageNum_invlist'];

                    }

                    $startRow_invlist = $pageNum_invlist * $maxRows_invlist;

                     

                    $colname_invlist = "-1";

                     

                     

                     

                     

                     

                    mysql_select_db($database_drama_database, $drama_database);

                     

                    if (isset($_POST['txt_search']))

                    {    //Only show what is being searched

                        $searchword = $_POST['txt_search'];

                        $query_invlist = "SELECT * FROM equipment WHERE name LIKE '%".$searchword."%' OR sublocation LIKE '%".$searchword."%' OR model LIKE '%".$searchword."%' OR manufacturer LIKE '%".$searchword."%' OR id LIKE '%".$searchword."%' OR description LIKE '%".$searchword."%' OR size LIKE '%".$searchword."%' OR location LIKE '%".$searchword."%' OR protection LIKE '%".$searchword."%' OR power_input LIKE '%".$searchword."%'";

                    }

                    else //show all records

                    {

                        $query_invlist = "SELECT * FROM equipment";

                    }

                    $query_limit_invlist = sprintf("%s LIMIT %d, %d", $query_invlist, $startRow_invlist, $maxRows_invlist);

                    $invlist = mysql_query($query_limit_invlist, $drama_database) or die(mysql_error());

                    $row_invlist = mysql_fetch_assoc($invlist);

                     

                     

                     

                     

                     

                     

                     

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

                      $totalRows_invlist = $_GET['totalRows_invlist'];

                    } else {

                      $all_invlist = mysql_query($query_invlist);

                      $totalRows_invlist = mysql_num_rows($all_invlist);

                    }

                    $totalPages_invlist = ceil($totalRows_invlist/$maxRows_invlist)-1;

                     

                    $queryString_invlist = "";

                    if (!empty($_SERVER['QUERY_STRING'])) {

                      $params = explode("&", $_SERVER['QUERY_STRING']);

                      $newParams = array();

                      foreach ($params as $param) {

                        if (stristr($param, "pageNum_invlist") == false &&

                            stristr($param, "totalRows_invlist") == false) {

                          array_push($newParams, $param);

                        }

                      }

                      if (count($newParams) != 0) {

                        $queryString_invlist = "&" . htmlentities(implode("&", $newParams));

                      }

                    }

                    $queryString_invlist = sprintf("&totalRows_invlist=%d%s", $totalRows_invlist, $queryString_invlist);

                    ?>

                      <style type="text/css">

                    .equiptitle {

                        font-size: xx-large;

                        font-weight: bold;

                    }

                    .titlelist {

                        font-size: xx-large;

                        font-weight: bold;

                    }

                    </style>

                     

                     

                     

                    <div align="right"></div>

                     

                    <div align="center">

                      <h1>Equipment Inventory</h1>

                    </div>

                    <div align="center">

                      <table width="1212" border="0" align="center">

                        <tr valign="top">

                          <td width="454" height="26"> Records  to  of

                    Records <?php echo ($startRow_invlist + 1) ?> to <?php echo min($startRow_invlist + $maxRows_invlist, $totalRows_invlist) ?> of <?php echo $totalRows_invlist ?> | <a href="equipmentinventory.php">Show All</a></td>

                          <td width="292" align="center">

                        

                    <input type="button" onclick="window.print()" value="Print Table" />

                        

                          </td>

                          <td width="452" align="right"><form id="search" name="search" method="post" action="">

                            <label for="txt_search">Search</label>

                            <input type="text" name="txt_search" id="txt_search" />

                            <input type="submit" name="btn_search" id="btn_search" value="Search" />

                          </form></td>

                        </tr>

                      </table>

                      <h4>

                    </div>

                    <?php if ($totalRows_invlist > 0) { // Show if recordset not empty ?>

                      <?php do { ?>

                        <div align="center"> <img src="images/divider.JPG" width="1085" height="19"><br>

                          <table width="1652" border="0">

                            <tr>

                              <td width="9"> </td>

                              <td width="271" height="173"><input name="image" type="image" src="<?php echo $row_invlist['image']; ?>" width="250" border="1"></td>

                              <td width="1358"><form name="form1" method="post" action="">

                                <table width="967" border="0">

                                  <tr>

                                    <td colspan="5" class="titlelist"><?php echo $row_invlist['name']; ?></td>

                                    <td width="169">Internal  ID: <strong><?php echo $row_invlist['id']; ?></strong></td>

                                  </tr>

                                  <tr>

                                    <td colspan="2"><em><?php echo $row_invlist['description']; ?></em></td>

                                    <td> </td>

                                    <td> </td>

                                    <td> </td>

                                    <td>External ID: <strong>E<?php echo $row_invlist['id']; ?></strong></td>

                                  </tr>

                                  <tr>

                                    <td width="131" height="146" rowspan="3" valign="top"><strong>Category:<br>

                                      Size:<br>

                                      Location:<br />

                                      Sub-Location: <br>

                                      Power Input Type:<br>

                                      Protection:<br>

                                    </strong><br></td>

                                    <td width="267" rowspan="3" valign="top"><?php echo $row_invlist['category']; ?><br>

                                      <?php echo $row_invlist['size']; ?><br>

                                      <?php echo $row_invlist['location']; ?><br />

                                      <?php echo $row_invlist['sublocation']; ?><br>

                                      <?php echo $row_invlist['power_input']; ?><br>

                                      <?php echo $row_invlist['protection']; ?><br></td>

                                    <td width="109" rowspan="3" valign="top"><strong>Model:<br />

                                      Manufacturer:</strong></td>

                                    <td width="230" rowspan="3" valign="top"><?php echo $row_invlist['model']; ?><br />

                                      <?php echo $row_invlist['manufacturer']; ?></td>

                                    <td width="35" rowspan="3"><img src="images/dividerhor.jpg" width="35" height="115"></td>

                                    <td height="38">Quantity: <strong><?php echo $row_invlist['quantity']; ?></strong></td>

                                  </tr>

                                  <tr>

                                    <td height="36"><a href="equipmentupdate.php?id=<?php echo $row_invlist['id']; ?>"><img src="images/updatebutton.png" width="71" height="25" />

                                      <input name="id" type="hidden" id="id" value="<?php echo $row_invlist['id']; ?>" />

                                    </a><a name="jump" id="jump"></a></td>

                                  </tr>

                                  <tr>

                                    <td height="38" align="left" valign="middle"><a onClick="return confirm('Are you sure you want to delete this piece of equipment?')" href="deleteequip.php?id=<?php echo $row_invlist['id']; ?>"><img src="images/deletebutton.png" width="71" height="25"></a></td>

                                  </tr>

                                </table>

                              </form></td>

                            </tr>

                          </table>

                        </div>

                        <?php } while ($row_invlist = mysql_fetch_assoc($invlist)); ?>

                      <?php } // Show if recordset not empty ?>

                    <br>

                    <?php if ($totalRows_invlist == 0) { // Show if recordset empty ?>

                      <div align="center">

                        <h3><img src="images/divider.JPG" alt="" width="1085" height="19" /><br />

                        No Results Found</h3>

                      </div>

                      <?php } // Show if recordset empty ?>

                    <br>

                    <div align="center"></div>

                    <?php

                    mysql_free_result($invlist);

                    ?>

                    • 7. Re: Jump to newly created record
                      Rob Hecker2 CommunityMVP

                      Even without the code snippet I provided, that script doesn't run, correct? I see a few fatal errors.

                       

                      Read up about the "while loop," use the DW balance braces feature to make sure your braces are all in pairs, and are the correct pairs.

                       

                      Realize that you are using the deprecated mysql connection method. Everyone should be using mySQLi or PDO.

                       

                      You should sanitize your url parameters, for security reasons. For instance:

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

                      . . .that just checkes to see if it is set. If the parameter is a number, then use is_numeric

                      if (is_numeric($_GET['totalRows_invlist'])) {

                      otherwise, use an assortment of other methods, including regex, to make sure the parameter is safe.