Current location: Hot Scripts Forums » Other Discussions » Database » info into array


info into array

Reply
  #11 (permalink)  
Old 07-09-09, 08:13 AM
zenix zenix is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Trophee

Jerry,

Are you a teacher of some sort? A lot of students could benefit from your apparent joy in teaching and the care you have in assisting others with their programming problems. YOU have helped me a lot, I am going to take the rest of this week and study your code and maybe even modify it a little to see what other possibilities exist for this database/user interface.

Thank you so much my friend for all your time and effort!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #12 (permalink)  
Old 07-09-09, 08:49 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by zenix View Post
Jerry,

Are you a teacher of some sort? A lot of students could benefit from your apparent joy in teaching and the care you have in assisting others with their programming problems. YOU have helped me a lot, I am going to take the rest of this week and study your code and maybe even modify it a little to see what other possibilities exist for this database/user interface.

Thank you so much my friend for all your time and effort!
Thank-You for the complement.
And no, I do not have any teaching degrees.
I just like to program, it helps calm my nerves.

I started programming when the only programming languages available were COBOL (COmmon Business-Oriented Language), FORTRAN (a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing) and BAL (Basic Assembly Language).

For the last 2 to 3 years I have been studying HTML, CSS, Javascript, PHP and MySql.
And I figured the easiest way to learn was to try to help others.
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #13 (permalink)  
Old 07-09-09, 10:48 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I found two more problems with the code, and had to add two more lines of code to correct them.

I believe now, I have worked out all the bugs.

PHP Code:

<?php
session_start
(); // Start or restart the session.
$sw=1;  // This variable used to toggle between the ADD and UPDATE buttons. Initally set to display the Add button.
$_SESSION["last_id"] = empty($_SESSION["last_id"]) ? $_SESSION["last_id"]; // This session variable used to store the current record id. It is set to 1 if there are no records in the database.

// Connect to the mysql server and create a database and a table if they don't already exist. //
$conn mysql_connect('localhost''root''')or die(mysql_error());
$create mysql_query("CREATE DATABASE IF NOT EXISTS db1") or die(mysql_error());
mysql_select_db ("db1");
$testdb 'CREATE TABLE IF NOT EXISTS testable(id int (11) NOT NULL auto_increment,
                             lname varchar(40) NOT NULL,
                             fname varchar(40) NOT NULL,
                               address varchar(40) NOT NULL,
                             address2 varchar(40) NOT NULL,
                             city varchar(15) NOT NULL,
                             state varchar(2) NOT NULL,
                             zip int(5) NOT NULL,
                             phone varchar(11) NOT NULL,
                             notes longtext NOT NULL,
                                PRIMARY KEY(id),
                             KEY testype(lname, fname))'
;

$result mysql_query($testdb) or die(mysql_error());

// Check to see if the session array has been populated.
// If it hasn't, then fetch any records from the database and populate it.
if(empty($_SESSION["id"][0]))
{
 
$results mysql_query("select * from testable") or die(mysql_error());
 while(
$r mysql_fetch_assoc($results))
 {
  
$_SESSION["id"][]=$_SESSION["last_id"]=$r["id"]; // $_SESSION["last_id"] will contain the last record id. If there are no records in the database, then it was previously set to 1.
  
$_SESSION["lname"][]=$r['lname'];
  
$_SESSION["fname"][]=$r['fname'];
  
$_SESSION["address"][]=$r['address'];
  
$_SESSION["address2"][]=$r['address2'];
  
$_SESSION["city"][]=$r['city'];
  
$_SESSION["state"][]=$r['state'];
  
$_SESSION["zip"][]=$r['zip'];
  
$_SESSION["phone"][]=$r['phone'];
  
$_SESSION["notes"][]=$r['notes'];
  
$sw=0// Display the Update button.
  
}
 }
else{
$sw=0;} // If the session array is already populated, then we need to display the Update button.

// Set the session array pointer to 0 to view the first record. Or set to current record being viewed.
$array_position = !empty($_POST["key"]) ? $_POST["key"] : 0;

// Check to see if the First button was pressed and set the session array pointer to the first record.
if(!empty($_POST["first"]))
{
 
$sw 0// This variable set to 0 so the Update button appears.
 
$array_position=0;
 }
 
// Check to see if the Next button was pressed.
if(!empty($_POST["next"]))
{
 
// Check to see if you are currently viewing a record.
 // If yes, then increment the session array pointer to the next record.
 // But not past the first empty array element.
 
if(isset($_SESSION["fname"][$array_position]))
 {
  
$array_position++;
  
// If the next record is not empty then display the Update button.
  
if(!empty($_SESSION["fname"][$array_position])){$sw 0;}
  else{
$sw=1;} // If it is empty, then display the Add button.
  
}
 }
 
// Check to see if the Prev button was pressed. If yes, then decrement the session array pointer to the previous record.
if(!empty($_POST["prev"]))
{
 
$sw 0// Display the Update button.
 
$array_position--;
 if(
$array_position<0){$array_position 0;} // Don't let the session array pointer get less than 0.
 
}

// Check to see if the Last button was pressed.
// If yes, then set the session array pointer to the last record.
if(!empty($_POST["last"]))
{
 
$sw 0// Display the Update button.
 
$array_position=count($_SESSION["id"])-1;
 }
 
// Check to see if the Update button was pressed.
// If yes, then update the record in the database and the session array.
if(!empty($_POST["update"]))
{
 
mysql_query("UPDATE testable SET lname = '".$_POST['lname']."',fname = '".$_POST['fname']."',address = '".$_POST['address']."',address2 = '".$_POST['address2']."',city = '".$_POST['city']."',state = '".$_POST['state']."',zip = '".$_POST['zip']."',phone = '".$_POST['phone']."',notes = '".$_POST['notes']."' WHERE id = '".$_SESSION["id"][$array_position]."'");
 
$_SESSION["lname"][$array_position]=$_POST['lname'];
 
$_SESSION["fname"][$array_position]=$_POST['fname'];
 
$_SESSION["address"][$array_position]=$_POST['address'];
 
$_SESSION["address2"][$array_position]=$_POST['address2'];
 
$_SESSION["city"][$array_position]=$_POST['city'];
 
$_SESSION["state"][$array_position]=$_POST['state'];
 
$_SESSION["zip"][$array_position]=$_POST['zip'];
 
$_SESSION["phone"][$array_position]=$_POST['phone'];
 
$_SESSION["notes"][$array_position]=$_POST['notes'];
 
$sw=0// Display the Update button.
 
}

// Check to see if the Add button was pressed.
// If yes, then add the record to the session array and insert the record into the database.
// By default $sw is previously set to 1 to display the Add button.
if(!empty($_POST["add"]))
{
 
$_SESSION["id"][]=$_SESSION["last_id"]++;
 
$_SESSION["lname"][]=$_POST['lname'];
 
$_SESSION["fname"][]=$_POST['fname'];
 
$_SESSION["address"][]=$_POST['address'];
 
$_SESSION["address2"][]=$_POST['address2'];
 
$_SESSION["city"][]=$_POST['city'];
 
$_SESSION["state"][]=$_POST['state'];
 
$_SESSION["zip"][]=$_POST['zip'];
 
$_SESSION["phone"][]=$_POST['phone'];
 
$_SESSION["notes"][]=$_POST['notes'];
 
$insert "INSERT INTO testable(lname,fname,address,address2,city,state,zip,phone,notes) VALUES('".$_POST['lname']."','".$_POST['fname']."','".$_POST['address']."','".$_POST['address2']."','".$_POST['city']."','".$_POST['state']."','".$_POST['zip']."','".$_POST['phone']."','".$_POST['notes']."')";
 
$results mysql_query($insert) or die ("Couldn't insert into database because: " .mysql_error());
 if(
mysql_affected_rows()==1)
 {
  echo 
'Data inserted successfully!';
  
$sw=0// A new record has been created so display the Update button.
  
}
 }
?>
<form name="form" method="post" action="#">
 <table>
  <tr><td align="right">First Name: </td><td><input type="text" name="fname" value="<?php echo $_SESSION["fname"][$array_position]; ?>"></td><td align="right">Last Name: </td><td><input type="text" name="lname" value="<?php echo $_SESSION["lname"][$array_position]; ?>"></td></tr>
  <tr><td align="right">Address: </td><td><input type="text" name="address" value="<?php echo $_SESSION["address"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Address2: </td><td><input type="text" name="address2" value="<?php echo $_SESSION["address2"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">City: </td><td><input type="text" name="city" value="<?php echo $_SESSION["city"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">State: </td><td><input type="text" name="state" value="<?php echo $_SESSION["state"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Zip: </td><td><input type="text" name="zip" value="<?php echo $_SESSION["zip"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Phone: </td><td><input type="text" name="phone" value="<?php echo $_SESSION["phone"][$array_position]; ?>"></td><td colspan=2></td></tr>
  <tr><td align="right">Notes: </td><td><textarea name="notes"><?php echo stripslashes($_SESSION["notes"][$array_position]); ?></textarea></td><td colspan=2></td></tr>
  <tr>
   <td>
    <?php
    
// If $sw is set to 1 then the Add button is displayed.
    
if($sw == 1)
    {
    
?>
     <input type="submit" name="add" value="Add">
    <?php
     
}
    
// If it is set to 0 then the Update button is displayed.
    
else
    {
    
?>
     <input type="submit" name="update" value="Update">
    <?php
     
}
    
?>
   </td>
   <td colspan=2 align="center" style="color:#00f;">
    Record <b>-</b>
    <?php
     
// Display the current or new record number.
     
echo $pos $array_position+1;
     if(
$sw == 0)
     {
      
// Display the total number of records if any exist.
      // Unless you are adding a record ($sw will equal 1). Then only display the new record number.
      
echo " of ".count($_SESSION["lname"]);
      }
    
?>
    </td>
   <td><input type="submit" name="first" value="First"><input type="submit" name="prev" value="Prev"><input type="submit" name="next" value="Next"><input type="submit" name="last" value="Last"></td>
  </tr>
 </table>
 <input type="hidden" name="key" value="<?php echo $array_position?>">
</form>
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #14 (permalink)  
Old 07-10-09, 06:42 AM
zenix zenix is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Jerry, Thank you again for all your help. I'll be printing this one out too for study. I have learned a lot from you my friend. It's kinda neat that you said you figure the best way to learn this was by helping others. I was told by both my math and my micro bio professors in college that the best way to learn is by helping others understand because it solidify's what you have already learned.

I hope someday to be able to assist others as you have me! Thank you so much again!

Garry
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #15 (permalink)  
Old 07-10-09, 07:44 AM
zenix zenix is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
I saw that in the start of the form you have "action ="#"". Is this just another way of saying action ="the current page.php"? That's kind of neat.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #16 (permalink)  
Old 07-10-09, 07:49 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by zenix View Post
I saw that in the start of the form you have "action ="#"". Is this just another way of saying action ="the current page.php"? That's kind of neat.
Yes that's exactly what it's for. Just don't leave it blank you can get unpredictable results.
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove value from array Nikas PHP 1 05-20-09 04:20 AM
Multi-Dimensional Array Help Nikas PHP 17 05-04-09 03:10 AM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 04:54 PM
Serializing a set of arrays dannyallen PHP 2 10-11-04 04:04 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM


All times are GMT -5. The time now is 02:33 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.