Help Required with printing an array from DB to the webpage.


#1

Hi there I am trying to make a search query output results to the web page that the user is viewing. I have 2 fields in the html, the search should query the db using either field if only one was entered or both if the two were entered…
Then I want to print the results to the screen, I’ve been told to use a foreach statement but am not sure how to use it so I have copied the php below:

[code]#!/usr/local/bin/php

<? session_start(); session_register('lname'); session_register('fname'); session_register('addr'); session_register('password'); session_register('in'); $in = "0"; //Check to see if 1 or both of fields were filled in if(!$_POST[lname] && !$_POST[addr]) { echo '


Please fill in the required fields before clicking the Find Customer button

'; echo 'TRY AGAIN!
'; exit; } if(!$_POST[lname]) { //setup names of database and table to use $db_name = "baz"; $table_name = "Customers"; //connect to server and select database $connection = @mysql_connect("mysql.internal", "baz", "rREfp2t4") or die(mysql_error()); //build and issue the query $sql = "SELECT * FROM $db_name.$table_name WHERE addr2 = '$_POST[addr]'"; $result = @mysql_query($sql, $connection) or die("result error: " . mysql_error()); $name = mysql_fetch_row($result); $fname = $name[0]; $lname = $name[1]; $addr1 = $name[2]; $addr2 = $name[3]; $addr3 = $name[4]; $addr4 = "Dublin" . $name[5]; $phNum = $name[6]; $mobNo = $name[7]; $email = $name[8]; exit; } else if(!$_POST[addr]) { //setup names of database and table to use $db_name = "baz"; $table_name = "Customers"; //connect to server and select database $connection = @mysql_connect("mysql.internal", "baz", "rREfp2t4") or die(mysql_error()); //build and issue the query $sql = "SELECT * FROM $db_name.$table_name WHERE l_name = '$_POST[lname]'"; $result = @mysql_query($sql, $connection) or die("result error: " . mysql_error()); $name = mysql_fetch_row($result); $fname = $name[0]; $lname = $name[1]; $addr1 = $name[2]; $addr2 = $name[3]; $addr3 = $name[4]; $addr4 = "Dublin" . $name[5]; $phNum = $name[6]; $mobNo = $name[7]; $email = $name[8]; exit; } else { //setup names of database and table to use $db_name = "baz"; $table_name = "Customers"; //connect to server and select database $connection = @mysql_connect("mysql.internal", "baz", "rREfp2t4") or die(mysql_error()); //build and issue the query $sql = "SELECT * FROM $db_name.$table_name WHERE addr2 = '$_POST[addr]' AND l_name = '$_POST[lname]'"; $result = @mysql_query($sql, $connection) or die("result error: " . mysql_error()); $name = mysql_fetch_row($result); $fname = $name[0]; $lname = $name[1]; $addr1 = $name[2]; $addr2 = $name[3]; $addr3 = $name[4]; $addr4 = "Dublin" . $name[5]; $phNum = $name[6]; $mobNo = $name[7]; $email = $name[8]; exit; } //Add fields to the database!! //Display the new updated results!!! ?> ABC Heating | 4th Year Project Add Staff Member  |  Delete Staff Member  |  Update Jobs List  |  Search For Customer Details  |   Change Customer Details



<font size=2 color="Black"

The Customers found matching your criteria are as follows:


First Name: <? echo "$fname"; ?>

Last Name: <? echo "$lname"; ?>

Address: <? echo "$addr1"; ?>
<? echo "$addr2"; ?>
<? echo "$addr3"; ?>
<? echo "$addr4"; ?>

Phone No: <? echo "$phNum"; ?>

Mobile No: <? echo "$mobNo"; ?>

E-mail Address: <? echo "$email"; ?>

Find Another Customer


[/code]

#2

Sorry but it sounds like you really don’t know what you are doing…

So here are a couple of tutorials to help get you on your way. Go to http://www.codewalkers.com and check out the tutorials/basics section. For the true beginner take a look at “Creating Dynamic websites with PHP and MySQL”. If you are looking for general programming information check out “A tour of Decision Making Structures”, “An Overview of Arrays”, “Control Structures”, “Databases and SQL”, “Strings Primer”, “Debugging”, “Using Sessions”, “Working with Forms”, and more.

If I am wrong about that and all you want is an explaination of the foreach statement here goes… Foreach takes a copy of each element in an array and allows you to act upon it as if it was it’s own variable. ( Manual page - http://www.php.net/foreach )

Code Example:

while ($row = mysql_fetch_row($resultID))
{
    foreach($row as $field)
    {
         // do something with $field
    }
}

Looking at your code though I would suggest you take a look at mysql_fetch_assoc instead ( Manual - http://www.php.net/mysql_fetch_assoc ). This will allow you to access the array with the database column names instead.


#3

Thanks for your reply,
It was the mysql_fetch_assoc that I needed. I am pretty new to this alright and wasn’t sure how to use the foreach statement as I wasn’t sure what it did exactly, think I know now and the code works so thanks…