How to pass the range of dynamic arrays through loop? - php

I have the retrieve the table through arrays, now I want to display that array in other table on the bases of user inputs like if user enter 2 then only 2 types tables will appear as well the data of table if user enter 3 three 3 tables with computed data display
Code of table retrieval
<?php
include('config.php');
$sa="select * from table1 where c13='$d'";
$result=mysql_query($sa) or die(mysql_error());
echo "<table border='1'>
<tr>
</tr>";
$row_count=0;
while($row = mysql_fetch_array($result))
{
echo $row['c1'];
echo $row['c2'];
echo $row['c3'];
echo $row['c4'];
$a[]=$row['c1'];
$b[]=$row['c2'];
$c[]=$row['c3'];
$d[]=$row['c4'];
$m[]=round(($row['c1']/$row['c4']),2);
$n[]=round(($row['c2']/$row['c4']),2);
$o[]=round(($row['c3']/$row['c4']),2);
$row_count++;
}
echo "Measuring table";
for($i=0;$i<$row_count;$i++)
{ // do the exploding, the imploding, the row echoing for each row//
echo "<table border='1' align='center'>
<tr>
<th>Inputs</th>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>";
echo "<tr>";
$f=implode($m,',');
$r=explode(',',$f);
print_r($f);
$f1=implode($n,',');
$r1=explode(',',$f1);
print_r($f1);
$f2=implode($o,',');
$r2=explode(',',$f2);
print_r($f2);
echo "<td>" ."W". "</td>";
echo "<td>".$r['0']. "</td>";
echo "</tr>";
echo "<td>" ."N". "</td>";
echo "<td>".$r1['$i']. "</td>";
echo "</tr>";
echo "<td>" ."D"."</td>";
echo "<td>".$r2['$i']. "</td>";
echo "</tr>";
echo "<td>" ."W". "</td>";
echo "<td>".$r['$i']. "</td>";
echo "</tr>";
echo "<td>" ."N". "</td>";
echo "<td>".$r1['$i']. "</td>";
echo "</tr>";
echo "<td>" ."D"."</td>";
echo "<td>".$r2['$i']. "</td>";
echo "</tr>";
</tables>
Instead of mentioning array index explicitly it should be display dynamically
result like this
c13=2(user enter value)
row1 2 3 4
row2 4 6 7
After retrieval
a
w 2
n 3
d 4 // this is of index 0//
w 4
n 6
d 7 // this is of index 1//
If now user enter 3, 3 rows will be in the database.
After retrieval
w
n
d
w
n
d
w
n
d
How can I do this dynamically?
I want that measuring table display dynamically bases on user input. Please help me

Just before starging the while loop that is retrieving the rows, declare a counter variable.
$row_count = 0;
while {
// loop here
$row_count++;
}
By the end of the row retrieval, $row_count will be holding the... well the row count
** Edit **
To properly echo the measuring table u should structure your code within a for loop
echo "Measuring table";
echo "<table border='1' align='center'>
<tr>
<th>Inputs</th>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>";
for($i=0;$i<$row_count;$i++) {
echo "<tr>";
$f=implode($m,',');
$r=explode(',',$f);
print_r($f);
$f1=implode($n,',');
$r1=explode(',',$f1);
print_r($f1);
$f2=implode($o,',');
$r2=explode(',',$f2);
print_r($f2);
echo "<td>" ."W". "</td>";
echo "<td>".$r[$i]. "</td>";
echo "</tr>";
echo "<td>" ."N". "</td>";
echo "<td>".$r1[$i]. "</td>";
echo "</tr>";
echo "<td>" ."D"."</td>";
echo "<td>".$r2[$i]. "</td>";
echo "</tr>";
}

Related

Html Table not echoing

i am trying this from 5hrs don't know i am going wrong.My table echos results but not echo total for the same.
<table width="102%" border="0">
<tr>
<th><b>Amount</b></th>
<th ><strong>County</strong></th>
</tr><?php
(this fetches data ...$row = mysqli_fetch_array($res) or die(mysqli_error($db)))
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "<td>" . $rui['county'] . "</td>";
echo "</tr>";
(I get these entreis correct )
}
}
(Below table is not echoed)
echo "<tr>";
echo "<td> .<b>Total </b>. </td>";
echo "<td>" .$x. "</td>";
echo "<td></td>";
echo "</tr>";
?></table>
Remove the } } after the closing tag of the tr.
if you can't close the whole PHP block then you probably did something wrong in your syntax.
I don't know if you have more code or not, if you do - post it so we will get more accurate view of your code.
<table width="102%" border="0">
<tr>
<th><b>Amount</b></th>
<th ><strong>County</strong></th>
</tr>
<?php
(this fetches data ...$row = mysqli_fetch_array($res) or die(mysqli_error($db)))
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "<td>" . $rui['county'] . "</td>";
echo "</tr>";
(I get these entreis correct )
(Below table is not echoed)
echo "<tr>";
echo "<td> .<b>Total </b>. </td>";
echo "<td>" .$x. "</td>";
echo "<td></td>";
echo "</tr>";
}
?>
</table>
You can remove the wrong bracket, OR check that the two bracket are opened when fetching data
<table width="102%" border="0">
<tr>
<th><b>Amount</b></th>
<th ><strong>County</strong></th>
</tr><?php
//(this fetches data ...$row = mysqli_fetch_array($res) or die(mysqli_error($db)))
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "<td>" . $rui['county'] . "</td>";
echo "</tr>";
//(I get these entreis correct )
//(Below table is not echoed)
echo "<tr>";
echo "<td> .<b>Total </b>. </td>";
echo "<td>" .$x. "</td>";
echo "<td></td>";
echo "</tr>";
?>
</table>

LEFT JOIN on PHP

I have tried the same code below on my c# app and it works fine, but when i tried the same code on php with the tweak of instead of column number on c# i replaced it with column name but the result is different.
$sql ="SELECT
`adexposure`.`symbol`,
`adexposure`.`netvolume`,
`fxexposure`.`netvolume`,
`adexposure`.`lastupdate`
FROM `adexposure`
LEFT JOIN `fxexposure` ON `adexposure`.`symbol` = `fxexposure`.`symbol`";
$result = $conn->query($sql);
if($result->num_rows>0)
{
echo "<table>";
echo "<tr>";
echo "<th>Symbol</th>";
echo "<th>Net Volume A</th>";
echo "<th>Net Volume B</th>";
echo "<th>Last Update</th>";
echo "</tr>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" .$row["symbol"]."</td>";
echo "<td>" .$row["netvolume"]."</td>";
echo "<td>" .$row["netvolume"]."</td>";
echo "<td>" .$row["lastupdate"]."</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "0 results";
}
Result should be:
<table border=1>
<tr>
<th>Symbol</th>
<th>Net Volume A</th>
<th>Net Volume B</th>
<th>Last Update</th>
</th>
</tr>
<tr>
<td>BITCOIN</td>
<td>2.5</td>
<td>3.5</td>
<td>2018.02.05 10:44</td>
</tr>
<tr>
<td>LITECOIN</td>
<td>1.5</td>
<td>5.5</td>
<td>2018.02.05 10:44</td>
</tr>
<tr>
<td>HASHCOIN</td>
<td>0.5</td>
<td>0.5</td>
<td>2018.02.05 10:44</td>
</tr>
</table>
but thats not the case.
The result shows both net volume of fxexposure.netvolume.
I hope you can help me with this.
Thanks...
just a suggestion you have two time netvolumn so you should use alias
sql ="SELECT
`adexposure`.`symbol`,
`adexposure`.`netvolume`,
`fxexposure`.`netvolume` as netvolume2,
`adexposure`.`lastupdate`
FROM `adexposure`
LEFT JOIN `fxexposure` ON `adexposure`.`symbol` = `fxexposure`.`symbol`";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" .$row["symbol"]."</td>";
echo "<td>" .$row["netvolume"]."</td>";
echo "<td>" .$row["netvolume2"]."</td>";
echo "<td>" .$row["lastupdate"]."</td>";
echo "</tr>";
}
If you assign an alias to the returned colums you can address them correctly in the php
$sql ="SELECT
`adexposure`.`symbol`,
`adexposure`.`netvolume` as `netvolume_A`, /* ALIAS */
`fxexposure`.`netvolume` as `netvolume_B`,
`adexposure`.`lastupdate`
FROM `adexposure`
LEFT JOIN `fxexposure` ON `adexposure`.`symbol` = `fxexposure`.`symbol`";
$result = $conn->query($sql);
if($result->num_rows>0)
{
echo "<table>";
echo "<tr>";
echo "<th>Symbol</th>";
echo "<th>Net Volume A</th>";
echo "<th>Net Volume B</th>";
echo "<th>Last Update</th>";
echo "</tr>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" .$row["symbol"]."</td>";
echo "<td>" .$row["netvolume_A"]."</td>";<!-- /* ALIAS */ -->
echo "<td>" .$row["netvolume_B"]."</td>";
echo "<td>" .$row["lastupdate"]."</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "0 results";
}
Read these two lines carefully:
echo "<td>" .$row["netvolume"]."</td>";
echo "<td>" .$row["netvolume"]."</td>";
You are using the same name to mean two different things, so there is no way for PHP to give a different result on those lines.
The solution is to give the values unique column aliases in your SQL:
`adexposure`.`netvolume` as netvolume_as,
`fxexposure`.`netvolume` as netvolume_fx,
And then those are the names in your PHP:
echo "<td>" .$row["netvolume_as"]."</td>";
echo "<td>" .$row["netvolume_fx"]."</td>";

Table creation with php and mysqli

i'm trying to do a table with php which takes the data from my database and build's the table im html but despite my while loop , only the 1st row is getting into the table. If i change the while loop before the
echo "<table border='1' cellpadding='2' cellspacing='2'";
I get all of them but in 3 tables.
What's wrong with the code ?
<?php
require 'connect.php';
$query = $link->query("SELECT * FROM fornecedor");
echo "Fornecedores";
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr>
<td>Nome</td>
<td>NIF</td>
<td>Cidade</td>
<td>Rua</td>
<td>NrPorta</td>
<td>Website</td>
<td>email</td>
</tr>";
while ($row = mysqli_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row["Nome"] . "</td>";
echo "<td>" . $row["NIF"] . "</td>";
echo "<td>" . $row["Cidade"] . "</td>";
echo "<td>" . $row["Rua"] . "</td>";
echo "<td>" . $row["NrPorta"] . "</td>";
echo "<td>" . $row["Website"] . "</td>";
echo "<td>" . $row["Email"] . "</td>";
echo "</tr>";
echo "</table>";
};
?>
Move echo "</table>"; out of the while loop-
....
while(){
.....
// don't close the table tag here
}
echo "</table>";
For each row you are adding the closing </table> tag which should not be like that.
echo "</table>"; code must be out of the while loop. It must be written once only.

Table formatting in PHP

I am trying to make a table in PHP.
All things are fine but all the rows are printed in same line.
How to print new line after each row in table? But in html there is no need to write extra code for new line why it is showing not a new line with PHP?
Here is that part of code:
<div class="contest-table" id="contest-table">
<table class="contest-details" id="contest-details">
<tr>
<th>CODE</th>
<th>NAME</th>
<th>START</th>
<th>END</th>
</tr>
<?php
//Show contest detials -> Contest Code|Contest Name |Start Date | End Date
$con=mysqli_connect("localhost","root","chandan","judge");
$result=mysqli_query($con,"SELECT * FROM judge_contest ");
echo "<tr>";
while($row = mysqli_fetch_array($result))
{
$contest_code=$row['contest_code'];
$contest_name=$row['contest_name'];
$contest_start_date=$row['start_date'];
$contest_end_date=$row['end_date'];
echo "<td>";
echo " $contest_code ";
echo "</td>";
echo "<td>";
echo " $contest_name ";
echo "</td>";
echo "<td>";
echo $contest_start_date;
echo "</td>";
echo "<td>";
echo $contest_end_date;
echo "</td>";
}
echo "</tr>";
?>
</table>
</div>
The problem is obvious, because you have put the statement echo "<tr>" and echo "</tr>" outside the while loop. You should put these two statement into the while loop.
echo '<tr>'
and
echo '</tr>'
should be inside the wile loop

i want to span columns in mysql result displayed on my page

hello everyone i have made a mysql table which consist of two columns and 35 rows,
now problem is i want to span or merge some of the columns in it but i don't know how to do it
here is my code
$result = mysqli_query($con,"SELECT * FROM Spec");
echo "<table border='1' id='spec'>
<tr>
<th class='th'>Engine</th>
<th class='th'>Spec</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='td1'>" . $row['Engine'] . "</td>";
echo "<td class='td'>" . $row['Spec'] . "</td>";
echo "</tr>";
}
if($row['Engine'] = $row['Suspension'])
{
echo "<td colspan='2'>" . $row['Suspension'] . "</td>";
}
echo "</table>";
mysqli_close($con);
There is problem with and in while loop, please update it accordingly.
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='td1'>" . $row['Engine'] . "</td>";
echo "<td class='td'>" . $row['Spec'] . "</td>";
echo "</tr>";
}
if($row['Engine'] = $row['Suspension'])
{
echo "<tr>";
echo "<td colspan='2'>" . $row['Suspension'] . "</td>";
echo "</tr>";
}
echo "</table>";
Several problems with your logic there and a couple of code issues. Since you haven't explained exactly what format your data is in and what layout you want to achieve I'll have a bash at both versions you can test them and see which one fits the layout you're after.
This version fixes the IF statement to correctly use comparitive operator of == not assignment of =
It also creates a new table row if the engine and suspension column values match so you'd end up with something looking like this
| ENGINE | SPEC |
| SUSPENSION |
$result = mysqli_query($con,"SELECT * FROM Spec");
echo "<table border='1' id='spec'>
<tr>
<th class='th'>Engine</th>
<th class='th'>Spec</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td class='td1'>" . $row['Engine'] . "</td>";
echo "<td class='td'>" . $row['Spec'] . "</td>";
echo "</tr>";
if($row['Engine'] == $row['Suspension']) {
echo "<tr><td colspan='2'>" . $row['Suspension'] . "</td></tr>";
}
}
echo "</table>";
mysqli_close($con);
Of course it relies that the contents of your Suspension column are exactly the same as the contents of your Engine column which I'll place a bet on they don't but you'll have to address you're logic about that
The other way of doing it is like this. This version will show | ENGINE | SPEC | or if your engine column matches your Suspension column it'll just show the | SUSPENSION | column instead.
$result = mysqli_query($con,"SELECT * FROM Spec");
echo "<table border='1' id='spec'>
<tr>
<th class='th'>Engine</th>
<th class='th'>Spec</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
if($row['Engine'] == $row['Suspension']) {
echo "<tr><td colspan='2'>" . $row['Suspension'] . "</td></tr>";
} else {
echo "<tr>";
echo "<td class='td1'>" . $row['Engine'] . "</td>";
echo "<td class='td'>" . $row['Spec'] . "</td>";
echo "</tr>";
}
}
echo "</table>";
mysqli_close($con);
Both will work though I suspect neither will provide you what you're looking for personally I don't think you need the IF() statement at all

Categories