Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Array/Foreach assistance with database query

Posted on 2008-06-12
7
Medium Priority
?
607 Views
Last Modified: 2013-12-13
I am wondering if there is a much more simpler way to pull info from a database and assign a variable to it and use that variable to echo info. What I have snipped works, im just tired of typing each field each time I create a new database.... I want to keep from having to type all the database fields.
Is this possible?
$query=" SELECT * FROM vendor WHERE id='{$_POST['id']}'";
 
$result=mysql_query($query);
$num=mysql_numrows($result);
 
$i=0;
while ($i < $num) {
 
$busniessname=mysql_result($result,$i,"businessname");
$primaryphoneareacode=mysql_result($result,$i,"primaryphoneareacode");
$primaryphoneprefix=mysql_result($result,$i,"primaryphoneprefix");
$primaryphonenumber=mysql_result($result,$i,"primaryphonenumber");
$ext=mysql_result($result,$i,"ext");
$street1=mysql_result($result,$i,"street1");
$street2=mysql_result($result,$i,"street2");
$primaryfaxareacode=mysql_result($result,$i,"primaryfaxareacode");
$primaryfaxprefix=mysql_result($result,$i,"primaryfaxprefix");
$primaryfaxnumber=mysql_result($result,$i,"primaryfaxnumber");
$primarycontact=mysql_result($result,$i,"primarycontact");
$primarycontactemail=mysql_result($result,$i,"primarycontactemail");
$city=mysql_result($result,$i,"city");
$estimatingcontact=mysql_result($result,$i,"estimatingcontact");
$estimatingcontactemail=mysql_result($result,$i,"estimatingcontactemail");
$state=mysql_result($result,$i,"state");
$ownerofficercontact=mysql_result($result,$i,"ownerofficercontact");
$ownerofficercontactemail=mysql_result($result,$i,"ownerofficercontactemail");
$zip=mysql_result($result,$i,"zip");
$=mysql_result($result,$i,"estimatingcontact");
$estimatingcontactemail=mysql_result($result,$i,"estimatingcontactemail");
 
++$i;
 
 
}?>

Open in new window

0
Comment
Question by:digarati
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21771599
I think you are looking for extract() function:
http://lu.php.net/manual/en/function.extract.php

$query=" SELECT * FROM vendor WHERE id='{$_POST['id']}'";
 
$result=mysql_query($query); 
while ($row = mysql_fetch_assoc())
{
  extract($row);   
  // do something here...
} 
?>

Open in new window

0
 
LVL 1

Author Comment

by:digarati
ID: 21771663
Im attaching my complete code to help....
$query=" SELECT * FROM goodco WHERE id='{$_POST['id']}'";
$result=mysql_query($query);
$num=mysql_numrows($result);
 
$i=0;
while ($i < $num) {
 
$job=mysql_result($result,$i,"job");
$commitment=mysql_result($result,$i,"commitment");
$owneramount=mysql_result($result,$i,"owneramount");
$vendoramount1=mysql_result($result,$i,"vendoramount1");
$cc1=mysql_result($result,$i,"cc1");
$cc2=mysql_result($result,$i,"cc2");
$ownerscope=mysql_result($result,$i,"ownerscope");
$vendorscope1=mysql_result($result,$i,"vendorscope1");
$vendorscope2=mysql_result($result,$i,"vendorscope2");
$signature=mysql_result($result,$i,"signature");
$ownerconumber=mysql_result($result,$i,"ownerconumber");
$vendorconumber=mysql_result($result,$i,"vendorconumber");
$authorize=mysql_result($result,$i,"authorize");
 
++$i;
 
 
}?>
<html>
 
 
 
<head>
<meta http-equiv="Content-Language" content="en-us">
</head>
 
<table border="0" width="100%" id="table1">
	<tr>
		<td>
		<p align="center"><b><font size="4">Entry Form</font></b></td>
	</tr>
</table>
 
 
 
</html>
<?
 
 
						echo "<table border=\"0\" cellpadding=\"\" cellspacing=\"4\">\n";
						echo "Owner Details<br/>";
						echo "<br/>";
						echo "<tr><td>Job:</td><td>".$job."</td>\n";
						echo "<tr><td>CO #:</td><td>".$ownerconumber."</td>\n";
						echo "<tr><td>Amount:</td><td>".$owneramount."</td>\n";
						echo "<tr><td>Ownerscope:</td><td>".$ownerscope."</td>\n";
						echo "<tr><td>Schedule Impact:</td><td>".$impact." Days</td>\n";
						echo "<tr><td>Owner Authorization:</td><td>".$authorize."</td>\n";
						echo "</table>";
?>
<html>
<body>
 
<p><img border="0" src="images/topbar.JPG" width="124" height="4"></p>
 
</body>
</html>
 
<?
						echo "<table border=\"0\" cellpadding=\"\" cellspacing=\"4\">\n";
						echo "<br/>";
						echo "Commitment Details<br/>";
						echo "<br/>";
						echo "<tr><td>Commitment:</td><td>".$commitment."</td>\n";
						echo "</table>";
						echo "<table border=\"0\" cellpadding=\"\" cellspacing=\"4\">\n";
						echo "<tr><td>CO #:</td><td>".$vendorconumber."</td>\n";
						echo "<tr><td>Distribution:</td><td></td><td></td>\n";
						echo "<tr><td></td><td>Cost Code</td><td>Amount</td><td>Scope</td>\n";
						echo "<tr><td></td><td>".$cc1."</td><td>".$vendoramount1."</td><td>".$vendorscope1."</td>\n";
						echo "<tr><td></td><td>".$cc2."</td><td>".$vendoramount2."</td><td>".$vendorscope2."</td>\n";
						echo "</table>";
?>
 
<html>
<form action="pmupdatecoprintstatus.php" method="POST">
<input type="hidden" name="timberprintdate" value="<? echo $printdate; ?>">
<input type="hidden" name="id" value="<? echo $id; ?>">&nbsp;<table border="0" width="100%" id="table1">
	<tr>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td>
		<p align="center">
		<img border="0" src="http://site.com/pm/images/Printers_and_Faxes.png" width="48" height="44">
		<img border="0" src="http://site.com/pm/images/E_Mail.png" width="37" height="38"></td>
	</tr>
	<tr>
		<td>
		<p align="center">Print Status &nbsp; <select size="1" name="timberprint">
	<option value="0">Not Printed</option>
	<option value="1">Printed</option>
			</select>&nbsp;
		<input type="Submit" value="Update"></td>
	</tr>
</table>
</form>
</html>

Open in new window

0
 
LVL 48

Expert Comment

by:hernst42
ID: 21771706
As you only retrieve one row you just can use:
$query=" SELECT * FROM goodco WHERE id='{$_POST['id']}'";
$result=mysql_query($query);
$num=mysql_numrows($result);
 
$row = mysql_fetch_assoc($result);
extract($row);
<html>
 ....

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:digarati
ID: 21771864
Now if i want to echo job, commitment within my table how to i specify, ......

<html>
<?
echo "<tr><td>Commitment:</td><td>".$commitment."</td>\n";
echo "<tr><td>Job:</td><td>".$job."</td>\n";
?>
</html>
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 21772242

$query=" SELECT * FROM goodco WHERE id='{$_POST['id']}'";
$result=mysql_query($query);
$num=mysql_numrows($result);
 
$row = mysql_fetch_assoc($result);
extract($row);
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
</head>
 
<table border="0" width="100%" id="table1">
        <tr>
                <td>
                <p align="center"><b><font size="4">Entry Form</font></b></td>
        </tr>
</table>
 
 
 
</html>
<?
 
 
                                                echo "<table border=\"0\" cellpadding=\"\" cellspacing=\"4\">\n";
                                                echo "Owner Details<br/>";
                                                echo "<br/>";
                                                echo "<tr><td>Job:</td><td>".$job."</td>\n";
                                                echo "<tr><td>CO #:</td><td>".$ownerconumber."</td>\n";
                                                echo "<tr><td>Amount:</td><td>".$owneramount."</td>\n";
                                                echo "<tr><td>Ownerscope:</td><td>".$ownerscope."</td>\n";
                                                echo "<tr><td>Schedule Impact:</td><td>".$impact." Days</td>\n";
                                                echo "<tr><td>Owner Authorization:</td><td>".$authorize."</td>\n";
                                                echo "</table>";
?>
<html>
<body>
 
<p><img border="0" src="images/topbar.JPG" width="124" height="4"></p>
 
</body>
</html>
 
<?
                                                echo "<table border=\"0\" cellpadding=\"\" cellspacing=\"4\">\n";
                                                echo "<br/>";
                                                echo "Commitment Details<br/>";
                                                echo "<br/>";
                                                echo "<tr><td>Commitment:</td><td>".$commitment."</td>\n";
                                                echo "</table>";
                                                echo "<table border=\"0\" cellpadding=\"\" cellspacing=\"4\">\n";
                                                echo "<tr><td>CO #:</td><td>".$vendorconumber."</td>\n";
                                                echo "<tr><td>Distribution:</td><td></td><td></td>\n";
                                                echo "<tr><td></td><td>Cost Code</td><td>Amount</td><td>Scope</td>\n";
                                                echo "<tr><td></td><td>".$cc1."</td><td>".$vendoramount1."</td><td>".$vendorscope1."</td>\n";
                                                echo "<tr><td></td><td>".$cc2."</td><td>".$vendoramount2."</td><td>".$vendorscope2."</td>\n";
                                                echo "</table>";
?>
 
<html>
<form action="pmupdatecoprintstatus.php" method="POST">
<input type="hidden" name="timberprintdate" value="<? echo $printdate; ?>">
<input type="hidden" name="id" value="<? echo $id; ?>">&nbsp;<table border="0" width="100%" id="table1">
        <tr>
                <td>&nbsp;</td>
        </tr>
        <tr>
                <td>
                <p align="center">
                <img border="0" src="http://site.com/pm/images/Printers_and_Faxes.png" width="48" height="44">
                <img border="0" src="http://site.com/pm/images/E_Mail.png" width="37" height="38"></td>
        </tr>
        <tr>
                <td>
                <p align="center">Print Status &nbsp; <select size="1" name="timberprint">
        <option value="0">Not Printed</option>
        <option value="1">Printed</option>
                        </select>&nbsp;
                <input type="Submit" value="Update"></td>
        </tr>
</table>
</form>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:digarati
ID: 21772347
getting this error with hernst42's solution

Warning: extract(): First argument should be an array in /home/content/h/a/l/site/html/beta/vendor_details.php on line 17
code snipped.
<?php
session_start(); 
////////////////////////////////////////////////////////////////////////////////////////////////////// SESSION STARTED
$link="<a href=\"http://www.site.com/login.html\">login</a>";
if(!isset($_SESSION["sessioname"])){
echo "You are not authorized to view this page! Please Login Here: $link";
}else{
////////////////////////////////////////////////////////////////////////////////////////////////////MY PROTECTED CONTENT
include 'header.php';
require_once "dbconnection.php";
 
$query=" SELECT * FROM vendor WHERE id='{$_POST['id']}'";
$result=mysql_query($query);
$num=mysql_numrows($result);
 
$row = mysql_fetch_assoc($result);
extract($row);
 
		
						echo "<table border=\"0\" cellpadding=\"\" cellspacing=\"4\">\n";
						echo "Owner Details<br/>";
						echo "<br/>";
						echo "<tr><td>Name:</td><td>".$businessname."</td>\n";
						echo "<tr><td>Contact:</td><td>".$primarycontact."</td>\n";
						echo "</table>";						
}?>

Open in new window

0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21773543
>Now if i want to echo job, commitment within my table how to i specify, ......
yes, you would specify $job, $commitment etc.
extract(array("job" => "Engineer, "commitment" => "full"));
will result in the variables $job and $commitment to be created and filled.
the mysql_fetch_assoc() will return an array of the field values with the field names...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question