newbie question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lazynewt
Forum Newbie
Posts: 15
Joined: Mon May 19, 2008 10:43 am

newbie question

Post by lazynewt »

Hi im trying to follow a guide to learn a bit about php coding. this is the coding i am trying to replicate.

Code: Select all

 
 
<?php
include 'library/config.php';
include 'library/opendb.php';
 
// if no CustomerID is specified, list the available customers
if(!isset($_GET['CustomerID']))
{
   $self = $_SERVER['PHP_SELF'];
 
   $query = "SELECT CustomerID, ContactFirstName FROM customers ORDER BY CustomerID";
   $result = mysql_query($query) or die('Error : ' . mysql_error());
 
   // create the article list
   $ContactLastName = '<ol>';
   while($row = mysql_fetch_array($result, MYSQL_NUM))
   {
      list($CustomerID, $ContactFirstName) = $row;
      $ContactLastName .= "<li><a href=\"$self?id=$CustomerID\">$ContactFirstName</a></li>\r\n";
      
   }
 
   $ContactLastName .= '</ol>';
 
   $ContactFirstName = 'customers';
} else {
   // get the article info from database
   $query = "SELECT ContactFirstName, ContactLastName FROM customers WHERE CustomerID=".$_GET['CustomerID'];
   $result = mysql_query($query) or die('Error : ' . mysql_error());
   $row = mysql_fetch_array($result, MYSQL_ASSOC);
 
   $ContactFirstName = $row['ContactFirstName'];
   $ContactLastName = $row['ContactLastName'];
}
 
include 'library/closedb.php';
?>
The guide is here http://www.php-mysql-tutorial.com/cms-php-mysql.php (not sure if its ok to post external links here but if not im sorry. Ive posted it to help people help me :).

anyway I am using

tablename = customers
id = CustomerID
title = ContactFirstName
content = ContactLastName

Now i know im probably missing something really stupid but if anyone can point it out id appreciate it. At first i thought that the words title and content may of been refering to something other than table headings. ive tried a few alterations but im shtuck.... thanks in advance for the help.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: newbie question

Post by aceconcepts »

Where did you get title and content from?
lazynewt
Forum Newbie
Posts: 15
Joined: Mon May 19, 2008 10:43 am

Re: newbie question

Post by lazynewt »

Thats the confusing thing about that tutorial site. they are using "title" and "content" as column titles. I would assume the correct code should be

Code: Select all

 
 
<?php
include 'library/config.php';
include 'library/opendb.php';
 
// if no CustomerID is specified, list the available customers
if(!isset($_GET['CustomerID']))
{
   $self = $_SERVER['PHP_SELF'];
 
   $query = "SELECT CustomerID, ContactFirstName FROM customers ORDER BY CustomerID";
   $result = mysql_query($query) or die('Error : ' . mysql_error());
 
   // create the article list
   $ContactLastName = '<ol>';
   while($row = mysql_fetch_array($result, MYSQL_NUM))
   {
      list($CustomerID, $ContactFirstName) = $row;
      $ContactLastName .= "<li><a href=\"$self?id=$CustomerID\">$ContactFirstName</a></li>\r\n";
      
   }
 
   $content .= '</ol>';
 
   $title = 'customers';
} else {
   // get the article info from database
   $query = "SELECT ContactFirstName, ContactLastName FROM customers WHERE CustomerID=".$_GET['CustomerID'];
   $result = mysql_query($query) or die('Error : ' . mysql_error());
   $row = mysql_fetch_array($result, MYSQL_ASSOC);
 
   $title = $row['ContactFirstName'];
   $content = $row['ContactLastName'];
}
 
include 'library/closedb.php';
?>
this didnt work for me though so i figured id post my first attempt. Am i still way off? I know this might look like a strange way for me to be learning php but im not the type to sit down with a book and learn the syntax until ive had a play with the code.

Thanks for any further help. Ill keep staring at the code and see if i can get anywhere myself but would appreciate some input.
lazynewt
Forum Newbie
Posts: 15
Joined: Mon May 19, 2008 10:43 am

Re: newbie question

Post by lazynewt »

Ok im going to try and break this down step by step to make sure im getting this right. Im pretty sre ive got this part right.

Code: Select all

<?php
include 'library/config.php';
include 'library/opendb.php';
 
// if no id is specified, list the available articles
if(!isset($_GET['CustomerID']))
{
   $self = $_SERVER['PHP_SELF'];
 
   $query = "SELECT CustomerID, ContactFirstName FROM Customers ORDER BY CustomerID";
   $result = mysql_query($query) or die('Error : ' . mysql_error());
ok i think this bits right

Code: Select all

  // create the article list
   $content = '<ol>';
   while($row = mysql_fetch_array($result, MYSQL_NUM))
   {
      list($CustomerID, $ContactFirstName) = $row;
      $content .= "<li><a href=\"$self?id=$CustomerID\">$ContactFirstName</a></li>\r\n";
   }
 
   $content .= '</ol>';
 
   $title = 'Available Articles';
} else {

argh im lost. I would of thought that this was right too. but im not getting any results. can someone give me a riddle or a clue please lol. :banghead:

Code: Select all

  // get the article info from database
   $query = "SELECT ContactFirstName, ContactLastName FROM customers WHERE CustomerID=".$_GET['CustomerID'];
   $result = mysql_query($query) or die('Error : ' . mysql_error());
   $row = mysql_fetch_array($result, MYSQL_ASSOC);
 
   $title = $row['title'];
   $content = $row['content'];
}
 
include 'library/closedb.php';
?>
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: newbie question

Post by aceconcepts »

I would re-write the last part of code like this:

Code: Select all

 
   // get the article info from database
   $CustomerID=$_GET['CustomerID'];
   $query = "SELECT ContactFirstName, ContactLastName FROM customers WHERE CustomerID='$CustomerID'";
   $result = mysql_query($query) or die('Error : ' . mysql_error());
   $row = mysql_fetch_array($result, MYSQL_ASSOC);
 
   $title = $row['title'];
   $content = $row['content'];
}
 
include 'library/closedb.php';
?>
 
However, this could effectively be the same. The only other exception could be your FORM method type - are you using POST or GET?
lazynewt
Forum Newbie
Posts: 15
Joined: Mon May 19, 2008 10:43 am

Re: newbie question

Post by lazynewt »

well for testing and to understand whats going on i am at the moment just trying to open this page and retrieve the customer data. My understanding was that this was a possability and a form was not nessecarry unless i am wanting to search for a specific record. I have tried the change but am still getting nothing. I understand what you have done there by shortening the code and imo thats a good idea. the result does seem to be the same which is boggeling my mind Surely i should have some kind of output or error....

=========edited===============
Sorry for my ignorance but i think i understand that a form "post" is needed for this to work.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: newbie question

Post by aceconcepts »

In your PHP code you posted you are using GET to retrieve you form data. If you use POST as your form's METHOD then this will not work.

Show me your form and we'll see what's happening.
lazynewt
Forum Newbie
Posts: 15
Joined: Mon May 19, 2008 10:43 am

Re: newbie question

Post by lazynewt »

Hi Ace i think ive got it now. This is the script i am using.

Code: Select all

 
 
<?php
include 'library/config.php';
include 'library/opendb.php';
 
// if no id is specified, list the available articles
if(!isset($_GET['CustomerID']))
{
   $self = $_SERVER['PHP_SELF'];
 
   $query = "SELECT CustomerID, ContactFirstName FROM Customers ORDER BY CustomerID";
   $result = mysql_query($query) or die('Error : ' . mysql_error());
 
   // create the article list
   $content = '<ol>';
   while($row = mysql_fetch_array($result, MYSQL_NUM))
   {
      list($CustomerID, $ContactFirstName) = $row;
      $content .= "<li><a href=\"$self?id=$CustomerID\">$ContactFirstName</a></li>\r\n";
   }
 
   $content .= '</ol>';
 
   $title = 'Available Articles';
} else {
    // get the the customer info from database
    $CustomerID=$_GET['CustomerID'];
   $query = "SELECT ContactFirstName, ContactLastName FROM customers WHERE CustomerID='$CustomerID'";
    $result = mysql_query($query) or die('Error : ' . mysql_error());
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
  
    $title = $row['title'];
    $content = $row['content'];
 }
  
 include 'library/closedb.php';
 ?>
 
 
 ?>
<html>
<head>
<title>
<?php echo $title; ?>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
 
// ... some css here to make the page look nicer
 
</style>
</head>
<body>
<table width="600" border="0" align="center" cellpadding="10" cellspacing="1" bgcolor="#336699">
<tr>
<td bgcolor="#FFFFFF">
<h1 align="center"><?php echo $title; ?></h1>
<?php
echo $content;
 
// when displaying a customer record show a link
// to see the customer list
if(isset($_GET['id']))
{
?>
<p>&nbsp;</p>
<p align="center"><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Article List</a></p>
<?php
}
?>
</td>
</tr>
</table>
</body>
</html>
It is displaying the records in a list which is great. I did not have a form in place yet but what i failed to realise is that i was not printing the information to the screen. So in reality the code was working fine. The above has also helped me understand that a title and content is needed and how to use php to display them. I am now going to make a form and see if i can get a search to work using it. i think i will use the post method as that is what i have used mostly so far in php. I understand that i will need to change the code above to use the post method and will see if i am at least capable of doing that. I will drop the form code here in my next post. Thanks for your help so far. And i know im seeming a bit noobish with php at the moment but ill get there in the end. thanks again.

p.s are there any advantages to using post over get or request ? or should i be using them depending on the scenario? thanks.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: newbie question

Post by aceconcepts »

There are pros and cons for each really. POST is often used because it does not send values via the url.

It's an interesting topic because forms are used so much - look it on the net and see what you can find.

I'm glad its working :D
Post Reply