Display multiple values from different rows with same column value - php

My code:
<?php
require_once('auth.php');
require_once('connection.php');
$value1 = $_POST['value1'];
$qry=mysql_query("SELECT * FROM table WHERE (`value1` LIKE '%".$value1."%')") or die(mysql_error());
if(mysql_num_rows($qry) > 0){
while ($result = mysql_fetch_array($qry)){
$value1 = $result['value1'];
$value2 = $result['value2'];
}}else{die ("can't find ".$value1."");}
?>
<br />
<table width="700" border="0">
<tr>
<td colspan="2" align="center"><strong>Info for <?php echo $value1; ?></strong></td>
</tr>
<tr>
<td width="100">Some text:</td>
<td width="590"><?php echo $value2;?></td>
</tr>
The code is working great when there is only one row with the same value of value1. If there are 2,3 or more rows with the same value for value1 the script displays only the values from the last row in my table found with the queried value. I want it to display separate tables with entries from all queried rows.
I've searched online for help, but all I could find is how to retrieve the values from my database, nothing helpful for me

you should move your html block to inside the while loop. try this
<br />
<table width="700" border="0">
<?php
if(mysql_num_rows($qry) > 0){
while ($result = mysql_fetch_array($qry)){
$value1 = $result['value1'];
$value2 = $result['value2'];
?>
<tr>
<td colspan="2" align="center"><strong>Info for <?php echo $value1; ?></strong></td>
</tr>
<tr>
<td width="100">Some text:</td>
<td width="590"><?php echo $value2;?></td>
</tr>
<?php
}}else{die ("can't find ".$value1."");}
?>
</table>
I moved the <table> code to before the while loop, moved the table rows to inside the loop and the table close tag after the loop. this way you get all rows.
If you don't want to show the table at all if there is no rows you can move the <table ..> and </table> tags to inside the if statement block.
Edit:
if(mysql_num_rows($qry) > 0){
?>
<br />
<table width="700" border="0">
<?php
while ($result = mysql_fetch_array($qry)){
$value1 = $result['value1'];
$value2 = $result['value2'];
?>
<tr>
<td colspan="2" align="center"><strong>Info for <?php echo $value1; ?></strong></td>
</tr>
<tr>
<td width="100">Some text:</td>
<td width="590"><?php echo $value2;?></td>
</tr>
<?php
}
echo '</table>';
}else{die ("can't find ".$value1."");}
?>

You're assigning values from the returned rows to $value1 and $value2. So each iteration of the loop replaces the last stored values in these variables.
This will effectively only give you the last row values found (last values assigned to the variables.)
You either need to push these values into a collection/array and re-loop through them to build your table, or put the table code in the initial loop.

Related

why does simple_html_dom find('tr')[0] get table row 2 instead of table row 1?

why does find('tr')[0]; get table row 2 instead of table row 1 ?
This is my html all tables have the same class and layout.
<table class="tablemenu">
<tbody>
<tr>
<td><b>hello</b></td>
<td><b>hi</b></td>
</tr>
<tr>
<td>hey</td>
<td>Alright</td>
<td>Good</td>
<td>Good</td>
<td><a>Date</a></td>
</tr>
</tbody>
</table>
<table class="tablemenu">
<tbody>
<tr>
<td><b>hello</b></td>
<td><b>hi</b></td>
</tr>
<tr>
<td>hey</td>
<td>Alright</td>
<td>Good</td>
<td>Good</td>
<td><a>Date</a></td>
</tr>
</tbody>
</table>
<table class="tablemenu">
<tbody>
<tr>
<td><b>hello</b></td>
<td><a>hi</a></td>
</tr>
<tr>
<td>hey</td>
<td>Alright</td>
<td>Good</td>
<td>Good</td>
<td><a>LINK</a></td>
</tr>
</tbody>
</table>
This is my php
<?php
include("simpleHtmlDom/simple_html_dom.php");
$html = new simple_html_dom();
// Load a file
$html->load_file('http://mySite.net/');
foreach($html->find('table[class=tablemenu]') as $element){
$Link = $element->find('tr')[0]->find('td')[4]->find('a')[0];
echo($Link->text());
echo '<br />';
}
?>
At first to get the word 'Date' i tried
$Link = $element->find('tr')[1]->find('td')[4]->find('a')[0];
But that didn't work, it said undefined index.
Then i tried this just messing around and it works
$Link = $element->find('tr')[0]->find('td')[4]->find('a')[0];
This gets the word Date for some reason. I don't understand why, i do need that but
although it works - i now can't access table row 1. to grab the word say "hi".
I see two one issues:
Your first <tr> only has 2 <td>s, so $element->find('tr')[0]->find('td')[4] should throw an exception.
Edit OP fixed pasted code.
Fix your markup. You're not properly closing your <tr> elements:
<table class="tablemenu">
<tbody>
<tr>
<td><b>hello</b></td>
<td><b>hi</b></td>
</tr> <!-- close this! --->
<tr>
<td>hey</td>
<td>Alright</td>
<td>Good</td>
<td><a>Date</a></td>
</tr> <!-- close this! --->
</tbody>
</table>
There is wrong indexing because you are not closing the tr tags properly
the link should be on first index instead of zeroth index
$Link = $element->find('tr')[1]->find('td')[4]->find('a')[0];
To print hi try
echo $element->find('tr')[0]->find('td')[1]->find('b')[0]->text();
Full code
foreach($html->find('table[class=tablemenu]') as $element){
$Link = $element->find('tr')[1]->find('td')[4]->find('a')[0];
echo($Link->text());
echo '<br />';
echo $element->find('tr')[0]->find('td')[1]->find('b')[0]->text();
}
If the above not works then find tr in tbody like
$Link = $element->find('tbody')->find('tr')[1]->find('td')[4]->find('a')[0];
Also for debugging, try this
foreach($html->find('table[class=tablemenu]') as $element){
echo '<pre>';
var_dump($element);// find the object here
echo '</pre>';
}

If IDs from two different tables are equal, display name from another table

I'm writing a code for my little admin panel, and since I'm not that advanced of a coder, I'm experiencing some troubles with getting a name using two different tables.
Here's my code so far:
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
include 'db_connect.php';
$sql = "SELECT * FROM $tbl_name WHERE is_dead='0'";
$result=mysql_query($sql);
?>
<title>Title</title>
<center><img src="header.png"></center>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<? include 'menu.php';?>
</tr>
<tr>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>Unique ID</strong></td>
<td align="center"><strong>Model</strong></td>
<td align="center"><strong>Last Online</strong></td>
<td align="center"><strong>Options</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['id']; ?></td>
<td><? if ($rows['unique_id'] == 7815684) { echo '<font color="blue"><b><u>7815684</u></b></font>'; }
elseif ($rows['unique_id'] == 2312964) { echo '<font color="blue"><b><u>2312964</u></b></font>'; }
else { echo $rows['unique_id']; } ?></td>
<td><? echo $rows['model']; ?></td>
<td align='center'><font color="green"><b><? echo $rows['last_updated']; ?></b></font></td>
<td align="center">update
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
So what I'm trying to do is to get user name, using two tables $tbl_name and $prtalbe using their unique_id. So, if unique_id from $tbl_name equals unique_id from $prtable, I want to show user's name from $prtalbe.
I've been trying another sql query:
$sql = "SELECT * FROM $tbl_name, $prtable WHERE $tbl_name.unique_id = $prtable.unique_id;
$result=mysql_query($sql);
Then doing while loop to get it working
while($rows=mysql_fetch_array($result)){
$rows['name'];
}
and it did work actually, but it didn't want to put it right into my code, since ID from $prtable and $tbl_name are different.
Try this:
$sql = "SELECT $prtable.username FROM $tbl_name INNER JOIN $prtable ON ($tbl_name.unique_id = $prtable.unique_id)";
When you call INNER JOIN you are fetching all rows from each table and combining them given the ON condition. For more information, see this: http://www.w3schools.com/sql/sql_join_inner.asp

MYSQL Output - HTML Table

I have a query to output data within my MYSQL database and would like it to display in a table. With "Name" and "UserID" as the headings... currently the code I have developed, does output the results but instead creates multiple instances of the table and doesn't create new rows.
Please could someone help me achieve my goal?
echo"
<table width='400' border='1'>
<tr>
<td>Name</td>
<td>SystemID</td>
</tr>
<tr>
<td> ".$row['FullName']." </td>
<td> ".$row['UserID']." </td>
</tr>
</table>";
The Table should be defined outside of the loop of the data, you just loop through the data and add a row on every loop :
echo "<table width='400' border='1'> ";
echo "<tr>
<td>Name</td>
<td>SystemID</td>
</tr>";
// Example of while loop :
while ($row = mysql_fetch_array($results)) {
echo" <tr>
<td> ".$row['FullName']." </td>
<td> ".$row['UserID']." </td>
</tr>";
}
echo "</table>";
If the code you have shown is within some kind of while loop that loops through the database results, it should be clearly evident why you get a bunch of one row tables. Your entire table output would happen with each loop execution. What you should be doing is outputting your table opening tag and header row before you start the data retrieval loop. Output only the row with each loop execution, and then output the table closing tag after you exit the loop.
echo"
<table width='400' border='1'>
<tr>
<td>Name</td>
<td>SystemID</td>
</tr>";
while ($row = $result->fetch_assoc()) { // or whatever your retrieval code is
echo "<tr>
<td> ".$row['FullName']." </td>
<td> ".$row['UserID']." </td>
</tr>";
}
echo "</table>";
You need to echo the table open tag and headers then loop through the database results, then echo the table closing tag.
You should put <table width='400' border='1'>
<tr>
<td>Name</td>
<td>SystemID</td>
</tr>
before loop (
<HERE>
while() {
...
}
AND </table> you should put after cycle...
it should look like this:
echo "
<table width='400' border='1'>
<tr>
<td>Name</td>
<td>SystemID</td>
</tr>";
while(....) {
echo "
<tr>
<td> ".$row['FullName']." </td>
<td> ".$row['UserID']." </td>
</tr>
";
}
echo "</table>";
http://www.siteground.com/tutorials/php-mysql/display_table_data.htm
Use this inside the loop instead
echo"
<tr>
<td> ".$row['FullName']." </td>
<td> ".$row['UserID']." </td>
</tr>";
and put the rest of the table wrapping around the loop.
Don't complicate the mission ; you don't need to do loop ; mysql provides option whichi is --html , just use it :
mysql --html -uroot -e "USE mydb;select * from mytable"
if you want to display only : FullName and UserID :
mysql --html -uroot -e "USE mydb;select FullName,UserID from mytable"

PHP Dynamically returning multidimensional arrays

I have been doing a lot of work in learning how to return multidimensional arrays dynamically- but what I can't seem to figure out is how to nest them.
I have two tables, each has the identical format: ID, name.
Table one: SSC
- sscid
- sscname
Table two: SRV
- srvid
- srvname
What I am trying to do is print all of the items in table two under EACH item in the table one list.
The table one items are the headers, the table two items are returned as a checkbox (with the srvid as the value) and label(srvname).
I can get it to all print together, but it is a. one giant list of results and it's in a
| checkbox | table 1: name | table 2: name | format.
Not pretty at all (although it is progress for me to get this far).
After I run my query and get the result, my code looks like this:
Now, I've had a few additional thoughts about the design of the concept re:the database tables go, but everything I read indicates that they really need to be on their own tables, and they should be able to be referenced by the key from one table and the key from the other (eventually ended up in a joint table with user ID references) Because they are numerically indexed, I don't know why this would be an issue for me; however I simply can't seem to get this to work properly.
I should mention that when I alter the code to try to make the ssc_name span 2 cols and make it more like a header, it returns a header row for each checkbox/srv row, instead of for all of the checkbox/srv rows.
if($result) {
echo '<table border="1" align="center" cellspacing="3" cellpadding="3" width="300">
<tr><th colspan="2"><h3>Options</h3></th></tr>
<tr><td></td><td align="left"><b>Services</b></td></tr>';
$numfields = mysql_num_fields($result);
$data = array();
$flist = array();
for($i=0;$i<$numfields;$i++)$flist[] = mysql_field_name($result,$i);
$data[0] = $flist;
while($row = mysql_fetch_assoc($result)) {
$data[] = $row;
echo '<tr><td colspan="2" align="center"><b>' . $row['ssc_name'] .'</b><td></tr>
<tr><td align="center"><input type="checkbox" value="'. $row['ssv_id'] .'" / </td>
<td align="left">' . $row['ssvname'] . '</td>
</tr>';
}
echo '</table>';
}
Can anyone help me figure this out, please?
You are missing a > on this line
<tr><td align="center"><input type="checkbox" value="'. $row['ssv_id'] .'" / </td>
Should be
<tr><td align="center"><input type="checkbox" value="'. $row['ssv_id'] .'" /></td>
Something you might be able to pick up on with better formatting...
<?php
if ($result) {
echo <<<EOD
<table border="1" align="center" cellspacing="3" cellpadding="3" width="300">
<tr>
<th colspan="2"><h3>Options</h3></th>
</tr>
<tr>
<td></td>
<td align="left"><b>Services</b></td>
</tr>
EOD;
while ($row = mysql_fetch_assoc($result)) {
echo <<<EOD
<tr>
<td colspan="2" align="center"><b>{$row['ssc_name']}</b><td>
</tr>
<tr>
<td align="center"><input type="checkbox" value="{$row['ssv_id']}" /></td>
<td align="left">{$row['ssvname']}</td>
</tr>
EOD;
}
echo '</table>';
}
?>
And I'm not really sure what business any of those arrays have being in there.

PHP MySQL populating values from database

lets say i retrieve all of the values where their position belongs to top8.I populate them out in a table and instead of displaying different kinds of values , it displays 3 tables with 3 different values, how is this so? any help so that different values belonging to certain values will all be displayed out? i only need one table with 3 different values.
<?
$facebookID = "top8";
mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select_db("schoutweet") or ie(mysql_error());
$data= mysql_query("SELECT schInitial FROM matchTable WHERE position='".$facebookID."'")
or die(mysql_error());
while($row = mysql_fetch_array($data))
{
?>
<center>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_bracket">
<tr>
<td class="brack_under cell_1"><a href="www.facebook.com"/>team 1.1><?= $row['schInitial']?><a/></td>
<td class="cell_2"> </td>
<td class="cell_3"> </td>
<td class="cell_4"> </td>
<td class="cell_5"> </td>
<td class="cell_6"> </td>
</tr>
<tr>
<td class="brack_under_right_up">team 1.2><?= $row['schInitial']?></</td>
<td class="brack_right"><!--1.2.1--></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td class="brack_right"><!--2.1--></td>
<td class="brack_under"><!--3.1--></td>
<td><!--here?--></td>
<td><!--there?--></td>
<td><!--everywhere?--></td>
</tr>
</table>
</center>
<?
}
?>
</body>
That's because your <table> tag is within the loop! Place the <table> tag outside the while loop.
place your table tags outside the while loop
Because your writing the table tag inside the while loop. Everything inside the loop is done each loop cycle. If you only want to have one table in the output, you'll have to open and close the table outside of the loop, like this:
$data= mysql_query("SELECT schInitial FROM matchTable WHERE position='".$facebookID."'")
or die(mysql_error());
?>
<center>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_bracket">
<?
while($row = mysql_fetch_array($data))
{
?>
<tr>
<td class="brack_under cell_1"><a href="www.facebook.com"/>team 1.1><?= $row['schInitial']?><a/></td>
<td class="cell_2"> </td>
<td class="cell_3"> </td>
<td class="cell_4"> </td>
<td class="cell_5"> </td>
<td class="cell_6"> </td>
</tr>
<tr>
<td class="brack_under_right_up">team 1.2><?= $row['schInitial']?></</td>
<td class="brack_right"><!--1.2.1--></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td class="brack_right"><!--2.1--></td>
<td class="brack_under"><!--3.1--></td>
<td><!--here?--></td>
<td><!--there?--></td>
<td><!--everywhere?--></td>
</tr>
<?
}
?>
</table>
</center>
That will, however, print three rows per loop and therefore per record (but you have references to the table contents in two of them, so I suppose that's what you want?).
Also take care about some not well-formed HTML you have there (e.g. the > character in the expression team 1.1> / team 1.2>. If you want to print the > character to the browser, encode it as HTML entity (> for this case). You also have a probably superfluous </ in the first column of the second row (</</td>).
you need to echo the HTML part as well in the while loop like
echo '<table>';

Categories