LEFT JOIN on PHP - 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>";

Related

Why Session Variable Sending only last row data to another page

here the problem is i need to view the whole row data in view details page when the button is clicked but when i click the last row data is sent through session variable and last row is displayed but i need particular row to display fully when the name is selected of the employee. please help me out in this .
thank you
index.php
<?php
include('connection.php');
$sql = "SELECT empname,salary,contact_no,department,empdesg FROM add_emp ORDER BY empname ASC ";
$result = $con->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
?>
<center>
<table cellpadding="20px" cellspacing="40px;" border="2px" align="center" width="device">
<tr>
<th>Name</th>
<th>Department</th>
<th>Designation</th>
<th> </th>
<?php
while($row = $result->fetch_assoc())
{
$name='';
$name=$row["empname"];
$department=$row["department"];
$designation=$row["empdesg"];
?>
</tr>
<td>
<?php echo $name?>
</td>
<td>
<?php echo $department?>
</td>
<td>
<?php echo $designation?>
</td>
<td><a href="viewdetails.php">
<button>Details</button>
</a>
<?php
$_SESSION['name']= $name;
?>
</td>
<?php
}
}
else {
echo "0 results";
}
?>
viewdetails.php
<?php
session_start();
include('connection.php');
$sql="select * from add_emp where empname='$_SESSION[name]'";
$result = mysqli_query($con, $sql); // First parameter is just return of "mysqli_connect()" function
echo "<br>";
echo " <table cellpadding='5px' cellspacing='10px' border='1px' align='center'>";
while ($row = mysqli_fetch_assoc($result))
{ // Important line !!! Check summary get row on array ..
echo "<tr>";
echo "<th>" ."Name". "</th>";
echo "<td>" .$row['empname']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."DOB". "</th>";
echo "<td>" .$row['eage']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Adhaar Number". "</th>";
echo "<td>" .$row['adhaar']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Address". "</th>";
echo "<td>" .$row['address']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Salary". "</th>";
echo "<td>" .$row['salary']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Phone Number". "</th>";
echo "<td>" .$row['contact_no']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Department". "</th>";
echo "<td>" .$row['department']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Designation". "</th>";
echo "<td>" .$row['empdesg']. "</td>";
echo "</tr>";
echo "<tr>";
}
echo "</table>";
?>

Data from Database into the table (can't figure out how to do layout)

This is my code (horrible one):
<?php
include 'connect/con.php';
$result = mysqli_query($con,"SELECT id, vidTitle FROM newsvid");
$result1 = mysqli_query($con,"SELECT imgCover, vidSD FROM newsvid");
$result2 = mysqli_query($con,"SELECT published FROM newsvid");
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result1)) {
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td >" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
mysqli_close($con);
?>
</body>
</html>
The question is how to show data from database in this layout:
--------------------------------
-id----------vidTitle-----------
--------------------------------
-imgCover------vidSD------------
--------------------------------
----------published-------------
So every time I will add more data , another block like I showed before will add up under existing one.
........................................................................................
There's no need to write 3 queries. You could do that with only one select, and then put all the echos inside a while. That way you're writing, it would run all the ids and titles first, then it would put a table after the table, with cover and vidSD.
Try to make a single query:
SELECT id, vidTitle, imgCover, vidSD, published FROM newsvid
That way you will have, on each row returned from database, all the information about the same row.
Now, running a while is the same as you're doing, just adapting some HTML:
echo "<table width='600' border='1'><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'>" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
You may want to order it too. Adding ORDER BY id DESC, would do that.

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

How to pass the range of dynamic arrays through loop?

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>";
}

PHP loop - creating a table using a loop and giving it headers

I am a few stages further in learning PHP but I have come to another annoying pit stop. I have a really simple bit of code that retrieves book items from my database. I am displaying them in an html table however because it is a loop, if I use the th tags for table header I get a header above every single data item!
Here is my code extract: (as you can see I have put my th tags as comments as that doesn't work)
<table border="0">
<br />
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
//echo "<tr>";
//echo "<th>";
//echo "Book Title";
//echo "</th>";
//echo "<th>";
//echo "Book Author";
//echo "</th>";
//echo "<th>";
//echo "Book Publisher";
//echo "</th>";
//echo "<th>";
//echo "Book ISBN";
//echo "</th>";
//echo "</tr>";
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
Move those echos out of your loop. Also, you shouldn't have a <br /> directly inside of a <table> tag.
Move your table header code outside of the loop.
IDIOT! Sorry guys....
Needed to put the th tags outside of the loop.... simple I know but easy to miss when your learning!
[=
Simply take the header outside the loop, so echo before you begin your loop but after the opening<table>
You have to move the headers above the loop:
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>"
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
echo "<td>";
...
What is static, stays static.
Wjat is dynamic, becomes PHP

Categories