Basic php form help (2) - php

This is an extension of a question I had yesterday.
I am trying to make a little php calculator that will show how much people can save on their phone bills if they switch to VoIP, and how much they can save with each service.
I have a form that will spit out the right amount for a monthly bill here:
http://www.nextadvisor.com/voip_services/voip_calculator.php?monthlybill=50&Submit=Submit
But now I need to integrate that with some other data and put in a table. The prices for each of the different services are in another file called "values.php". The values are:
$offer1calcsavings="24.99";
$offer2calcsavings="20.00";
$offer3calcsavings="21.95";
$offer4calcsavings="23.95";
$offer5calcsavings="19.95";
$offer6calcsavings="23.97";
$offer7calcsavings="24.99";
I want each of the seven rows of the table to have one of the offercalcsavings values subtracted from the monthlybill value.
The php code currently looks like this:
<?php $monthlybill = $_GET['monthlybill']; ?>
Your monthly bill was <?php echo "$monthlybill"; ?>
<?php
$monthybill="monthlybill";
$re=1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
while($offername!=""){
$offerlo ='offer'.$re.'logo';
$offerlogo=${$offerlo};
$offerli ='offer'.$re.'link';
$offerlink=${$offerli};
$offeran ='offer'.$re.'anchor';
$offeranchor=${$offeran};
$offerst ='offer'.$re.'star1';
$offerstar=${$offerst};
$offerbot='offer'.$re.'bottomline';
$offerbottomline=${$offerbot};
$offerca ='offer'.$re.'calcsavings';
$offercalcsavings=${$offerca};
echo '<tr >
<td >
<a href="'.$offerlink.'" target="blank">
<img src="http://www.nextadvisor.com'.$offerlogo.'" alt="'.$offername.'" />
</a>
</td>
<td ><span class="rating_text">Rating:</span>
<span class="star_rating1">
<img src="http://www.nextadvisor.com'.$offerstar.'" alt="" />
</span><br />
<div style="margin-top:5px; color:#0000FF;">
Go to Site
<span style="margin:0px 7px 0px 7px;">|</span>
Review
</div> </td>
<td >'.$offercalcsavings.'</td>
</tr>';
$re=$re+1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
}
?>

I want each of the seven rows of the
table to have one of the
offercalcsavings values subtracted
from the monthlybill value.
You need to do the math somewhere. Presumably you would do this before the echo statement where you output your row.
$offerwithsavings = $monthlybill - $offercalcsavings
Then just be sure to include it in your table somewhere.
<td >'.$offerwithsavings.'</td>
The real prescription for this code is an array and foreach() loop, but I thought I'd keep my answer simple for now. Arrays and foreach() loops are very powerful and relatively quick and easy to learn. You would do well to give them some in depth study.

You can include the values.php file like this:
include 'path/to/values.php';
If you put the values in values.php in an array you can easily loop through them using a foreach loop:
in values.php;
$values['1calsavings'] = 24.99;
$values['2calsavings'] = 20.00;
etc;
Then in the other file:
include 'path/to/values.php';
foreach($values as $name => $amount){
echo '<some_markup>';
echo "$name $amount";
echo '</some_markup>';
}

Egads.
Use arrays!
So:
$offercalcsavings[0] = "24.99";
$offercalcsavings[1] = "20.00";
etc. etc.
Then, you're looping the output really:
for($i = 0; $i < CONSTANT_THAT_EQUALS_7; $i++) {
echo "<html bits>";
echo $offercalcsavings[$i];
echo $offerlink[$i];
etc. etc.
}

Related

Display PHP info in a table based layout

I am trying to display a 'meet the team' page in a 2x5 table, working with code written by the previous web designer in Wordpress.
This is the page I am looking at - http://www.stirling-house.com/about-us/meet-the-team As I say, I want to display the pictures in a table, so it looks neater.
The code I have is -
<div id="maintext">
<h1>MEET THE TEAM</h1>
<h3>THE STIRLING HOUSE ADMINISTRATION TEAM</h3>
<?php $members =get_posts('numberposts=99&cat=4&order=ASC'); //print_r($members);
foreach ($members as $post) {
setup_postdata($post);?>
<div class="member-box">
<img src="<?php echo get('member_photo');?>" alt="" style="margin:5px 7px 0 0;" />
<h3><?php echo $post->post_title;?></h3>
<h4><?php echo get('member_designation');?></h4>
Can anyone help?!
One way of doing it is by adding a counter before the foreach (for example, $n), initialize it to zero and increment it every time the loop finishes. Then, make all the odd divs float to the left and the even ones float to the right.
This is how the code would look like (more or less):
<?
$n = 0; // Counter
foreach($members as $post){
if($n % 2) $style="float:left";
else $style="float:right";
<img src="<?php echo get('member_photo');?>" alt="" style="margin:5px 7px 0 0;" />
<h3><?php echo $post->post_title;?></h3>
<h4><?php echo get('member_designation');?></h4>
If that does not work, you can try using a table layout and placing a <tr> tag after $n rows (resetting the counter every time you add a <tr>).
It is the opinion of most web designers (post-2005 or so) that <table> elements should not be used for layout/design. If you add the following CSS rule to your sites, stylesheet:
.member-box { display: inline-block; }
You will get something that looks like this: http://imgur.com/DzEkLAU
Sorry to answer indirectly, but I think this is the answer you're really looking for... ;)

Writing php code based on SQL count

If you look at the jsfiddle you'll notice modules in yellow and red. I have all my code working except that I have to manually input the html/php for the modules.
http://jsfiddle.net/W6zYA/1/
My module code is:
<?php $row = mysql_fetch_row($result); ?>
<a href='#' data-reveal-id="<?php echo $row[0]; ?>" data-animation="none" >
<div class="grid_3">
<p id="contexttitle">
<?php echo $row[1]; ?>
<span style="float:right;"> <?php echo $row[3]; ?> </span>
</p>
<p id="accesssubmenu">Last Update: <?php echo $row[6]; ?> </p>
</div>
</a>
<div id="<?php echo $row[0]; ?>" class='reveal-modal'>
<h1> <?php echo $row[1]; ?> </h1>
<p> <?php echo $row[4]; ?> </p>
<p4>Last Update: <?php echo $row[6]; ?> </p4>
<a class="close-reveal-modal">×</a>
</div>
What I'm wanting to do is have it create X amount of these modules based on a SQL Query where I count the # of rows in each section (red, yellow, green).
I tried to store the php in the SQL database and after research found out that this is bad practice and shouldn't be done even when using eval() as there will be user input. I also read about PHP templates such as Smarty but wasn't certain if that was the correct solution for me.
If someone can point me in the right direction I'd greatly appreciate it I'm just not sure where to start/what to google.
mysql_fetch_row only fetches one row. If you have multiple rows you need mysql_fetch_assoc or mysql_fetch_array and use something like a foreach loop to loop through the results. This will output the html as many times as results in the MySQL query.
Try mysql_num_rows($result) that will give you the number of rows returned by the query. I would try to upgrade to PDO or mysql since the mysql_* functions are going to be deprecated in PHP 5.5
http://php.net/manual/en/function.mysql-num-rows.php
I assume, that you get multiple records from you database? That way you can use a while loop to loop trough your result.
while ($row = mysql_fetch_row($result)) {
// do you html code here.
}
That should do the trick.

Submitting values of "jQuery Editable Invoice" into a MySQL DB using PHP

I am using this fantastic example of a jQuery editable invoice as a template for creating dynamic invoices for my users.
It's working quite well and I am successfully generating the items but I now need to save the values entered into the various text fields and enter them into the MySQL database.
I am confident in doing the MySQL entering with PHP but what makes this trickier is that the amount of 'invoice items' is completely dynamic and I am unsure how I can get PHP to 'check' through the pages text fields and find new ones, group them and then add them to my DB.
Here is an example of my code that I am using to generate the items:
<?php if($invoice_items->result_array()) { ?>
<?php foreach($invoice_items->result_array() as $invoice_Row): ?>
<tr class="item-row">
<td class="item-name">
<div class="delete-wpr">
<textarea><?php echo $invoice_Row['item_name']; ?> Facility Booking</textarea>
<a class="delete" href="javascript:;" title="Remove row">X</a>
</div>
</td>
<td class="description">
<textarea><?php echo $invoice_Row['description']; ?></textarea>
</td>
<td><textarea class="cost">$<?php echo $invoice_Row['hourly_cost']; ?>.00</textarea></td>
<td><textarea class="qty"><?php echo $total_time_hours; ?></textarea></td>
<td><span class="price">$<?php $unit_total = $invoice_Row['hourly_cost']* $total_time_hours; echo $unit_total;?>.00</span></td>
</tr>
<?php endforeach; ?>
<?php } ?>
I am thinking that I need to perhaps generate unique ID's for each invoice items text field, ie item-1-desc, item-1-cost etc, but that involves writing javascript which I know almost nothing about. Also I would still have to get PHP to loop through the ID's somehow until it reached the end...
If anyone has attempted something similar before or you can see a solution to my problem I would greatly appreciate your help.
Thanks,
Tim
Use the php form array syntax name="item-desc[<?php echo $id?>]"
You can then iterate them on the backend with foreach to store the data. You have the id's as keys to the arrays so it should be fairly trivial to update the db.

Dynamic data population in table form with Code Igniter

I have a pretty simple question, but for some reason I am drawing a blank. I have the following code in my view file, and I want to display the results in a two column table, so the first entry would be on the left, the next would be on the right then the next one after that would be below the first row, and eventually I will use the pagination class (haven’t gotten that far yet) For some reason I can not figure out how to get the results to display in a 2 column format… only one. Any help would be greatly appreciated.
Ideally I would like to have 4 columns, but the code below was started with just the idea of 2 columns.
Thanks!
<table>
db->query($sql);
foreach ($query->result() as $row)
{
echo("");
echo("");
echo $row->Title;
echo ("<br/>");
?>
<img name="<?php echo $row->Thumb;?>" src="../uploaded/portfolio/thumbs/<?php echo $row->Thumb;?>" alt="">
<?php
echo("<br/>");
echo $row->DescText;
echo("</td>");
echo("<td>");
// Display next picture here
echo("</td>");
echo("</tr>");
}
?>
../
Your code example is rather confusing, but I think from your description that you're trying to do something like this:
<table>
<tr>
<?php $i = 0; foreach($query->result() as $row): ?>
<?php if ($i % 2 == 0): ?>
</tr><tr>
<?php endif; ?>
<td>
<?php //whatever you want to put in your column goes here; ?>
</td>
<?php $i++; endforeach; ?>
</tr>
</table>
If you want the table to be four rows across, just change the "if ($i % 2 == 0)" to "if ($i % 4 == 0)".

basic php form help

I'm trying to make a calculator that will take inputs from users and estimate for them how much money they'll save if they use various different VoIP services.
I've set it up like this:
<form method="get" action="voip_calculator.php">
How much is your monthly phone bill?
<input name="monthlybill" type="text" value="$" size="8">
<p><input type="submit" name="Submit" value="Submit">
</p>
</form>
On voipcalculator.php, the page I point to, I want to call "monthlybill" but I can't figure out how to do it. I also can't figure out how to make it do the subtraction on the numbers in the rows.
This may be very simple to you but it's very frustrating to me and I am humbly asking for a bit of help. Thank you!
Here is the relevant stuff from voip_calculator, you can also click on the url and submit a number to see it in (in)action. I tried various times to call it with no success:
<table width="100%;" border="0" cellspacing="0" cellpadding="0"class="credit_table2" >
<tr class="credit_table2_brd">
<td class="credit_table2_brd_lbl" width="100px;">Services:</td>
<td class="credit_table2_brd_lbl" width="120px;">Our Ratings:</td>
<td class="credit_table2_brd_lbl" width="155px;">Your Annual Savings:</td>
</tr>
Your monthly bill was <?php echo 'monthlybill' ?>
<?php echo "$monthlybill"; ?>
<?php echo "monthlybill"; ?>
<?php echo '$monthlybill'; ?>
<?php echo 'monthlybill'; ?>
<?php
$monthybill="monthlybill";
$re=1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
while($offername!="") {
$offerlo ='offer'.$re.'logo';
$offerlogo=${$offerlo};
$offerli ='offer'.$re.'link';
$offerlink=${$offerli};
$offeran ='offer'.$re.'anchor';
$offeranchor=${$offeran};
$offerst ='offer'.$re.'star1';
$offerstar=${$offerst};
$offerbot='offer'.$re.'bottomline';
$offerbottomline=${$offerbot};
$offerca ='offer'.$re.'calcsavings';
$offercalcsavings=${$offerca};
echo '<tr >
<td >
<a href="'.$offerlink.'" target="blank">
<img src="http://www.nextadvisor.com'.$offerlogo.'" alt="'.$offername.'" />
</a>
</td>
<td >
<span class="rating_text">Rating:</span>
<span class="star_rating1">
<img src="IMAGE'.$offerstar.'" alt="" />
</span>
<br />
<div style="margin-top:5px; color:#0000FF;">
Go to Site
<span style="margin:0px 7px 0px 7px;">|</span>
Review
</div>
</td>
<td >'.$offercalcsavings.'</td>
</tr>';
$re=$re+1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
}
?>
offercal(1,2,3,4,5,6,7)savings calls to a file called values.php where they are defined like this:
$offer1calcsavings="24.99";
$offer2calcsavings="20.00";
$offer3calcsavings="21.95";
$offer4calcsavings="23.95";
$offer5calcsavings="19.95";
$offer6calcsavings="23.97";
$offer7calcsavings="24.99";
One thing i do is this
echo "<pre>";
print_r($_GET);
echo "</pre>";
Put this somewhere in your receiving end and you'll get an understanding of whats happening.
Not enough detail provided for an answer but let's simplify and assume you have the 'savings' numbers in an array, say, companySavings . So, you need to subtract each of these from the value the user specifies right? You don't need to call something (you could if you want...)
when the user clicks 'Submit' and the page is loaded again pull the monthlybill into a var e.g.
$monthlyBill = $_GET['monthlybill']; //you should do some checking to prevent attacks but that's another matter
Then, when you are building the list of savings it would look something like this
<?php
//...code for the rest of the page and starting your table
foreach($companySavings as $savings){//insert each row into the table
echo("<tr><td>".(comapnyName/Image whatever..)."</td><td>$".$monthlyBill-$savings."</td></tr>);
}
//... end the table and rest of code
?>
You need to get the value from the QueryString and put it into a PHP variable.
Like this:
$monthlyBill = $_GET['monthlybill'];
Now the variable $monthlyBill contains the value from the QueryString.
To display it:
echo "Your monthly bill is: $monthlyBill";

Categories