implementing a while loop help

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
bruisedghost
Forum Newbie
Posts: 16
Joined: Wed Jan 21, 2009 12:15 am

implementing a while loop help

Post by bruisedghost »

Hi all, Im newish to php and am confused if I should wrap this bit of code in a while loop. Basically my problem is that for this invoicing page I have created it currently will only accept single values e.g. any order with more than one item will only display one item. I need to get the $desc and $prod variables to loop, Any suggestions would be most appreciated!

Thanks!

Code: Select all

 if ($print_mode) {
    $output .= <<<EOD
<html>
  <head>
    <style type="text/css">
    
* {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 12px;
    font-style: normal;
    color: #333399;
}
 
</style>
  </head>
  <body>
EOD;
  }
//working uid select statement, to test echo uid  
$uid1= db_result(db_query("SELECT uid FROM ec_transaction WHERE txnid = $txn->txnid"));
//builds off of previous query this select statement will show all of the necessary info for the fields.  
$firstname = db_result(db_query("SELECT value, fid FROM profile_values where uid= $uid1 and fid = 1"));
$lastname = db_result(db_query("SELECT value, fid FROM profile_values where uid= $uid1 and fid = 4"));
$address1 = db_result(db_query("SELECT value, fid FROM profile_values where uid= $uid1 and fid = 6"));
$address2 = db_result(db_query("SELECT value, fid FROM profile_values where uid= $uid1 and fid = 7"));
$city = db_result(db_query("SELECT value, fid FROM profile_values where uid= $uid1 and fid = 8"));
$state = db_result(db_query("SELECT value, fid FROM profile_values where uid= $uid1 and fid = 9"));
$zip = db_result(db_query("SELECT value, fid FROM profile_values where uid= $uid1 and fid = 10"));
$gross = db_result(db_query("SELECT gross FROM ec_transaction WHERE txnid = $txn->txnid"));
//begins to build the node descriptioin query
$noder = db_result(db_query("SELECT nid FROM ec_transaction_product WHERE txnid = $txn->txnid"));
$desc = db_result(db_query("SELECT body FROM node_revisions WHERE nid = $noder"));
$prod = db_result(db_query("SELECT title FROM node_revisions WHERE nid = $noder"));
$email = db_result(db_query("SELECT mail from users WHERE uid= $uid1"));
$paybill = "https://www.trumpetconsulting.com/clientcenter/?q=store/payment/authorize_net/".$txn->txnid;
$test = db_result(db_query("SELECT title from ec_transaction_product where txnid = $txn->txnid"));
$output .= <<<EOD
 
   echo $row[test];
    }
 
<h2 style="margin-bottom:2px;">INVOICE DETAILS</h2>
<hr style="background-color:#ccc;color:#ccc;"><br />
 
<!-- BEGIN INVOICE WRAP-->
<div style="position:relative;padding:10px;border:1px solid #ccc;">
 
<img src="http://www.trumpetconsulting.com/images/invoice.png" width="171" height"48" alt="Invoice" style="float:right;" />
 
$payment_info<br><br>
<br />
<div style="position:relative;width:350px;border:1px solid #ccc;padding:10px;">
    <div style="position:absolute;top:-15px;left:5px;padding:5px;font-weight:bold;background-color:#fff;">Invoice To:</div>
    $firstname $lastname<br />
    $address1 $address2 <br />
    $city $state $zip <br />
    $email
</div>
 
<br />
<br />
<table cellpadding="5" style="width:635px;border:1px solid #ccc;">
  <tr>
    <td width="500" style="border-bottom:1px solid #ccc;background-color:#cecece;"><strong>Service Description</strong></td>
    <td width="135" style="border-bottom:1px solid #ccc;background-color:#cecece;"><strong>Price</strong></td>
  </tr>
  <tr>
    <td width="500" style="border-bottom:1px solid #ccc;">$prod<br /><div style="font-size:10px;line-height:13px;margin-top:5px;"><em>$desc</em></div></td>
    <td width="135" style="border-bottom:1px solid #ccc;border-left:1px solid #ccc;">&#36;&nbsp;$price</td>
  </tr>
</table>
 
<br />
 
 
<div style="position:relative;height:25px;">
    <div style="position:absolute;right:155px;top:2px;"><strong>TOTAL</strong></div>
    <div style="position:absolute;right:4px;top:0;width:140px;height:20px;border:1px solid #ccc;">&nbsp;&nbsp;&#36;&nbsp;$gross</div><br />
</div><br />
 
<div style="position:relative;height:25px;">
    <div style="position:absolute;right:4px;top:0;width:140px;height:20px;text-align:center;"><span style="font-size:14px;"><a href="$paybill">PAY MY BILL</a></span></div><br />
</div>
 
 
 
 
 
 
<br /><br />
 
</div><!--END INVOICE WRAP-->
 
 
<br />
<br />
 
<div style="position:relative;height:50px;">
<img src="http://www.trumpetconsulting.com/images/lock3.png" width="50" height="50" style="float:left;margin:0px 10px 30px 0px;" />
<p style="font-size:11px;line-height:16px;margin:0px 0px 0px 10px;text-align:justify;"><a href="$paybill">Click here to pay this bill</a> using our secure payment portal.  All information is secured with 128-bit encription.  The client center is verified with an SSL Certificate, and payments are processed using Authorize.net.  </p>
</div><br />
 
 
EOD;
 
if ($print_mode) {
  $output .= <<<EOD
    </body>
  </html>
EOD;
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: implementing a while loop help

Post by php_east »

i did not read your codes closely, but as i glanced i noticed you used this heredoc begin
at line 38, and then straight into a php command echo.

i don't think that is allowed within heredoc. also the close curly braces after that does not seem to have a beginning. you may want to look closely at this part again.

# $output .= <<<EOD
#
# echo $row[test];
# }
#
bruisedghost
Forum Newbie
Posts: 16
Joined: Wed Jan 21, 2009 12:15 am

Re: implementing a while loop help

Post by bruisedghost »

yeah I noticed that and removed it.
bruisedghost
Forum Newbie
Posts: 16
Joined: Wed Jan 21, 2009 12:15 am

Re: implementing a while loop help

Post by bruisedghost »

that little bit of code that was in thier was just for testing to see if the variable wold call properly. Im still having the problem where I need to display all the data in a row. should I use foreach()?
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: implementing a while loop help

Post by php_east »

yes, of course. a loop of some sort. while, for, foreach etc.
bruisedghost
Forum Newbie
Posts: 16
Joined: Wed Jan 21, 2009 12:15 am

Re: implementing a while loop help

Post by bruisedghost »

so my question is how do create a while loop with db_result(db_query()). would it be something like this

$test = db_result(db_query("SELECT gross FROM ec_transaction WHERE txnid = $txn->txnid"));

while ($test) { .....
}

?
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: implementing a while loop help

Post by php_east »

no that would put you infinity, you want something like

foreach ($test as $key=>$value)

but this isn't a loop over db access, it is loop over the results, which is better as you would want to minimise your db access as much as possible.

if you want to loop over the db, it be something like this

Code: Select all

 
if (mysql_num_rows($r) > 0) 
{
   while ($a = mysql_fetch_assoc($r)) 
     {
     $out[] = $a;
     }
   mysql_free_result($r);
 
i just took that example at random to illustrate the method used.

so it may be your wrapper (db_query) may not be suitable for looping, or there maybe a method defined specifically for looping, perculiar to your wrapper.

if you want a standard way to do it, the above example applies. if you want to us your existing wrapper, you would have to study it, as the return value is important if you want to loop safely and not get caught in an endless loop.
Post Reply