For loops needed to populate table. But I keep getting an extra two columns that repeat the data in the first two columns. What am I getting wrong with my for loops? All help is appreciated.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Calculating Interest</title>
<style>
table {
border: 3px solid black;
border-color: black;
text-align: left;
padding: 1px;
}
th {
background-color: #01008A;
color: white;
}
tr:nth-child(odd) {background-color: #AED8E6}
</style>
</head>
<body>
<table>
<tr>
<th>Year</th>
<th>Amount on deposit</th>
</tr>
<?php
for($i = 1; $i <= 10; $i++) {
echo "<tr>";
for ($j = 1; $j <= 2; $j++) {
$p = 1000;
$r = 0.05;
$n = 1;
echo "<td>" . $i . "</td>";
echo "<td>" . ($p = number_format(($p * pow((1 + $r), $i)), 2)) . "</td>";
$n++;
}
echo "</tr>";
}
?>
</table>
</body>
</html>
You have loop with $j that no needed! So use this:
<?php
for($i = 1; $i <= 10; $i++) {
echo "<tr>";
$p = 1000;
$r = 0.05;
$n = 1;
echo "<td>" . $i . "</td>";
echo "<td>" . ($p = number_format(($p * pow((1 + $r), $i)), 2)) . "</td>";
$n++;
echo "</tr>";
}
?>
Related
I am trying to get 2 simple for loops. The first for loop cycles through results from the database. The 2nd for loop prints out a table for each store based on the amount of weeks selected.
My code looks as follows
$retval = f_select_query($query_select_stores, $datarows);
$rowcount = count($datarows);
for ($counter = 0; $counter< $rowcount; $counter++) {
$store_name = $datarows[$counter]->store_name;
echo '<table style="width: 100%; border:1px solid black;">';
echo '<tr>';
echo '<th style="border: 1px solid black;">' . $store_name . '</th>';
for ($i=$week_number_start; $i<=$week_number_end; $i++){
echo '<th style="border: 1px solid black;>Week ' . $i . '</th>';
}
echo '</tr>';
echo '</table>';
}
And where the result is printed out, I get Week 2, Week 4, Week 6 and so on. Pretty much every 2nd week. Why is it doing that? I'm probably missing something very simple..
you have double quote missing in the seconde for loop, as Barmar mentionned, it's combining two elements.
so try this after add the missing double quote:
$retval = f_select_query($query_select_stores, $datarows);
$rowcount = count($datarows);
for ($counter = 0; $counter< $rowcount; $counter++) {
$store_name = $datarows[$counter]->store_name;
echo '<table style="width: 100%; border:1px solid black;">';
echo '<tr>';
echo '<th style="border: 1px solid black;">' . $store_name . '</th>';
for ($i=$week_number_start; $i<=$week_number_end; $i++){
echo '<th style="border: 1px solid black;">Week ' . $i . '</th>';
}
echo '</tr>';
echo '</table>';
}
I'm following along with the example from w3schools to extract some rows from a mysql db and return the results in a table format, but I'm only getting blank rows (although the correct number of them, oddly enough).
Here is what I have in my php script:
<!DOCTYPE html>
<html>
<head>
<style>
table
{
width: 100%;
border-collapse: collapse;
}
table, td, th
{
border: 1px solid black;
padding: 5px;
}
th
{
text-align: left;
}
</style>
</head>
<body>
<?php
$q = $_GET['q'];
$con = mysqli_connect('localhost', 'root', '123', 'drugsatfda');
if (!$con) {die('Could not connect: ' . mysqli_error($con));}
mysqli_select_db($con, "drugsatfda");
$sql="SELECT * FROM products WHERE DrugName LIKE '"."%".$q."%"."'";
$result = mysqli_query($con,$sql);
$count = 0;
echo "<table>
<tr>
<th>Application Number</th>
<th>Product Number</th>
<th>Form</th>
<th>Strength</th>
<th>Reference Drug</th>
<th>Drug Name</th>
<th>Active Ingredient</th>
</tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC) && $count < 10) {
echo "<tr>";
echo "<td>" . $row['ApplNo'] . "</td>";
echo "<td>" . $row['ProductNo'] . "</td>";
echo "<td>" . $row['Form'] . "</td>";
echo "<td>" . $row['Strength'] . "</td>";
echo "<td>" . $row['ReferenceDrug'] . "</td>";
echo "<td>" . $row['DrugName'] . "</td>";
echo "<td>" . $row['ActiveIngredient'] . "</td>";
echo "</tr>";
$count++;
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Can someone please help me see where I'm going wrong?
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC) && $count < 10) {
When doing this, you get two conditions, so $row = mysqli_fetch_array($result, MYSQLI_ASSOC) just becomes an expression returning a boolean true, so while $count is less than 10, you get while (true && true).
The solution is to break; when the loop reaches 10 instead.
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
if ($count >= 10) // If we reach 10 iterations, break the loop
break;
echo "<tr>";
echo "<td>" . $row['ApplNo'] . "</td>";
/* and so on */
echo "</tr>";
$count++;
}
You can verify this by doing
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC) && $count < 10) {
var_dump($row);
}
Which will output bool(true); for each iteration, until there are no more rows to fetch (when mysqli_fetch_array() returns null), or when $count is 10 or greater - whichever comes first.
The easier approach
An alternative, is simply just to fetch 10 rows. You can add a LIMIT clause to your SQL, like
$sql="SELECT * FROM products WHERE DrugName LIKE '%".$q."%' LIMIT 10";
This will fetch only 10 rows, meaning that you can just loop through it normally, without having to count
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['ApplNo'] . "</td>";
/* and so on */
echo "</tr>";
}
It should also be noted that your code is currently wide open to SQL injections, and you should use prepared statements to guard yourself against this.
References
http://php.net/manual/en/mysqli.prepare.php
How can I prevent SQL injection in PHP?
Please try again...
<!DOCTYPE html>
<html>
<head>
<style>
table
{
width: 100%;
border-collapse: collapse;
}
table, td, th
{
border: 1px solid black;
padding: 5px;
}
th
{
text-align: left;
}
</style>
</head>
<body>
<?php
$q = $_GET['q'];
$con = mysqli_connect('localhost', 'root', '123', 'drugsatfda');
if (!$con) {die('Could not connect: ' . mysqli_error($con));}
mysqli_select_db($con, "drugsatfda");
$sql="SELECT * FROM products WHERE DrugName LIKE '"."%".$q."%"."'";
$result = mysqli_query($con,$sql);
$count = 0;
echo "<table>
<tr>
<th>Application Number</th>
<th>Product Number</th>
<th>Form</th>
<th>Strength</th>
<th>Reference Drug</th>
<th>Drug Name</th>
<th>Active Ingredient</th>
</tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
if($count >=10)
break;
echo "<tr>";
echo "<td>" . $row['ApplNo'] . "</td>";
echo "<td>" . $row['ProductNo'] . "</td>";
echo "<td>" . $row['Form'] . "</td>";
echo "<td>" . $row['Strength'] . "</td>";
echo "<td>" . $row['ReferenceDrug'] . "</td>";
echo "<td>" . $row['DrugName'] . "</td>";
echo "<td>" . $row['ActiveIngredient'] . "</td>";
echo "</tr>";
$count++;
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
The following code reads a .csv file and generates an HTML table. I want to decide which column (field) will have all data aligned left or center. How can I do that?
<?php
$colhead = "#9BBB59";
$colrow = "#D7E4BC";
echo "<table style=\"text-align: left; margin-left: auto; margin-right: auto; font-family: Helvetica,Arial,sans-serif; border: 1px solid black; padding:1px; border-spacing: 1px; border-collapse: separate; frame=\"border\" rules=\"none\">";
echo "<tbody>";
$row = 1;
if (($handle = fopen("myfile.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo "<tr>";
}else{
echo "<tr>";
}
for ($c=0; $c < $num; $c++) {
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
// ------------- HEADER
echo "<td style=\"background-color:$colhead; text-align:center; vertical-align:middle; color:#ffffff; font-size:16px; font-weight:bold; padding:4px;\"> ".$value." </td>";
}else{
// ------------- GENERIC ROW
echo "<td style=\"background-color:$colrow; text-align:left; vertical-align:middle; color:#000000; font-size:14px; padding:4px; border:0px solid\"> ".$value." </td>";
}
}
if ($row == 1) {
echo '</tr>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
echo '</center>';
fclose($handle);
}
?>
<?php
$dom = new DomDocument();
$dom -> load("file.xml");
$data = $dom->getElementsByTagName('Data');
$counter = 0; // Set the entry counter
echo( "<table>");
foreach($data as $node) {
if ($counter % 3 == 0) {
echo '<tr>';
}
echo "<td>". $node -> textContent . "<td>";
if($counter % 3 == 0) {
echo '</tr>';
}
$counter++; // Increment the counter
}
echo( "</table>");
?>
It's not the cleanest code, but should work.
I have looked at all the posts I could find but they don't quite to seem to do it!
Any help appreciated.
Have inserted relevant php code here:
Previously having done the search and placed the number of matches in the variable $num_rows I then turn the relevant data into variables $displayurl and $displaytitle, then place them in a table cell. I want to display the data in $displayurl as a hyperlink.
$i = 0;
while ($i < $num_rows) {
$displayurl=mysql_result($result,$i,"url");
$displaytitle = mysql_result($result, $i, "pagetitle");
echo "<tr>";
echo "<td align = left>" .$displayurl. " </td>";
echo "<td align = left> " .$displaytitle. " </td>";
echo "</tr>";
$i++;
}
echo "<td align = left><a href='" .$displayurl. "'>link1</a></td>";
surround it by <A> tag
$i = 0;
while ($i < $num_rows) {
$displayurl=mysql_result($result,$i,"url");
$displaytitle = mysql_result($result, $i, "pagetitle");
echo "<tr>";
echo "<td align = left><a href='" .$displayurl. "'>link1</a> </td>";
echo "<td align = left> " .$displaytitle. " </td>";
echo "</tr>";
$i++;
}
You can also do it this way
echo "<td align = left><a href='$displayurl'>Click here</a></td>";
OR
echo "<td align = left>Click here</td>";
In the HTML, an anchor tag or link is written as Text that gets the underline
<?php
$i = 0;
while ($i < $num_rows) {
$displayurl = mysql_result($result, $i, 'url');
$displaytitle = mysql_result($result, $i, 'pagetitle');
echo '<tr>';
echo '<td align="left"> ' . $displayurl . ' </td>';
echo '<td align="left"> ' . $displaytitle . ' </td>';
echo '</tr>';
$i++;
}
I have a table of data that is generated dynamically based on the contents stored in a mysql database.
This is how my code looks:
<table border="1">
<tr>
<th>Name</th>
<th>Description</th>
<th>URL</th>
</tr>
<?php
$query = mysql_query("SELECT * FROM categories");
while ($row = mysql_fetch_assoc($query)) {
$catName = $row['name'];
$catDes = $row['description'];
$catUrl = $row['url'];
echo "<tr class=''>";
echo "<td>$catName</td>";
echo "<td>$catDes</td>";
echo "<td>$catUrl</td>";
echo "</tr>";
}
?>
</table>
Now if the table was static, then I would just assign each alternating table row one of 2 styles in repeated order:
.whiteBackground { background-color: #fff; }
.grayBackground { background-color: #ccc; }
and that would be the end of that. However since the table rows are dynamically generated, how can I achieve this?
Or you could just use CSS:
table tr:nth-child(odd) {
background-color: #ccc;
}
<?php
$x++;
$class = ($x%2 == 0)? 'whiteBackground': 'grayBackground';
echo "<tr class='$class'>";
?>
It basically checks to see if $x is divisible evenly by 2. If it is, it is even.
P.S. if you haven't seen that style of if else query, it is called a ternary operator.
Set a variable to true/false or a number and then back again during each iteration. Or use the modulus operator such as $i%2==0 in a while loop where $i is a number and use this condition in a ternary statement or something that sets the class value of the <tr>
Easiest way to alternate row colors in PHP/HTML?
$i = 0;
while ( $row = mysql_fetch_assoc($result) ) {
echo '<tr class="' . ( ( $i %2 == 0 ) ? 'oneValue' : 'anotherValue' ) . '"><td>' . $row['something'] . '</td></tr>';
$i++;
}
<?
$color="1";
while ($line = mysql_fetch_array($result)) {
if($color==1){
echo '<tr bgcolor="">';
$color="2";
} else {
echo '<tr bgcolor="#dcdcdc">';
$color="1";
}
?><td align="left" width="40"><?= $line[name] ?></td>
<?
}
?>
This is my working code !
Here is my working part !
`
$i=1;
while($row = mysqli_fetch_array($result)) {
if($i%2==0)
{
echo '<tr bgcolor="#FFFF00">';
}
else
{
echo '<tr bgcolor="#99FFCC">';
}
$i++;
echo "<td>" . $row['blah'] . "</td>";
echo "<td>" . $row['blah_blah'] . "</td>";
echo "</tr>";
}
echo "</table>";
`
This is how I did it. I declared a css class called "even" with all the styling i wanted. Then looped through the scenario. Hope it helps!
<?php
include 'connect.php';
echo "<table id='hor-zebra'>";
$i = 0;
while($row = mysql_fetch_array($result))
{
if($i%2 == 0)
{
echo "<tr class='even'>";
echo "<td>" . $row['something'] ." ". $row['something'] . "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row['something'] ." ". $row['something'] . "</td>";
echo "</tr>";
}
$i++;
}
echo "</table>";
mysql_close($con);
?>