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!
<?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';
?>
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.
<?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.
<?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());
// 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';
?>
// 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?
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.
<?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> </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.