PHP select only one entry shows - php

how come when I use this code:
$emailc = 'thomas990428#me.com_classes';
$emaila = 'thomas990428#me.com_assignments';
$emailp = 'thomas990428#me.com_projects';
$resulty = mysqli_query($con,"SELECT * FROM `$emailc` ORDER BY period"); if (!$result) echo mysqli_error(); else // ok, do your thing.
$resulti = mysqli_query($con,"SELECT * FROM `$emaila` ORDER BY duehw"); if (!$result) echo mysqli_error(); else // ok, do your thing.
$resulto = mysqli_query($con,"SELECT * FROM `$emailp` ORDER BY dueproj"); if (!$result) echo mysqli_error(); else // ok, do your thing.
$classcount = 1;
while($row = mysqli_fetch_array($resulty))
{
$period = $row['period'];
$teacher = $row['teacher'];
$subject = $row['subject'];
$subjecto = strtolower($subject);
$subjecto = str_replace(' ', '', $subjecto);
$grade = $rowy['grade'];
echo "<section id='" . $subjecto . "'> \n";
echo "<p class='title'>" . $subject . "</p> \n";
echo "<a style='cursor:pointer;' onclick='homework" . $classcount . "()'>Homework </a>|<a style='cursor:pointer;' onclick='projects" . $classcount . "()'> Projects</a> \n";
echo "<div id='homework" . $classcount . "'><br /><a onclick='addassignment()'>Add Assignment</a><br />";
echo "<table class='homework'>";
echo "<tr>";
echo "<th class='title'>";
echo "Title";
echo "</th>";
echo "<th class='duedate'>";
echo "Due Date";
echo "</th>";
echo "</tr>";
while($row = mysqli_fetch_array($resulti))
{
$subjecthw = $row['subjecthw'];
$namehw = $row['namehw'];
$duehw = $row['duehw'];
echo "<tr>";
echo "<td class='title'>";
echo $namehw;
echo "</td>";
echo "<td class='duedate'>March ";
echo $duehw;
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div> \n";
echo "<div id='projects" . $classcount . "'><br /><a onclick='addassignment()'>Add Assignment</a><br />";
echo "<table class='homework'>";
echo "<tr>";
echo "<th class='title'>";
echo "Title";
echo "</th>";
echo "<th class='duedate'>";
echo "Due Date";
echo "</th>";
echo "</tr>";
while($row = mysqli_fetch_array($resulto))
{
$subjectproj = $row['subjectproj'];
$nameproj = $row['nameproj'];
$dueproj = $row['dueproj'];
echo "<tr>";
echo "<td class='title'>";
echo $nameproj;
echo "</td>";
echo "<td class='duedate'>March ";
echo $dueproj;
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div> \n";
echo "</section> \n";
$classcount += 1;
}
I only get the hw and proj in one section. Am I doing it wrong? Thanks! I know that it probably will never work, but how do I fix it? I have 3 tables and I need to get data from all of them.

Don't use $row within the while loop.
e.g
while($row = mysql_fetch_row($resulti)){
while($row2 = mysql_fetch_row($resulto)){
}
}
Also didn't have time to go through the code but it's never a good idea to run SQL in a loop.

Related

Drupal SQL table how to insert edit button on each row

I've got a table in Drupal with the following code:
$db = mysql_connect("localhost", "root", "moocow");
mysql_select_db("vedb", $db);
$result = mysql_query("SELECT NodeID,NodeDesc,NodeZone,Fusion,DSLID FROM `nodeidtable` WHERE DSLID != '' AND `NodeZone` = 'CLOSED' ORDER BY NodeID ASC");
$num_rows = mysql_num_rows($result);
echo "<table>";
echo "<tr>";
echo "<th>CLOSED SITES</th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "</tr>";
echo "<tr>";
echo "<th>Node ID</th>";
echo "<th>Node Address</th>";
echo "<th>Node Zone</th>";
echo "<th>Fusion Status</th>";
echo "<th>Service Number</th>";
echo "</tr>";
//display the data
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
echo "<br>";
echo "<tr>";
echo "</table>";
mysql_free_result($result);
mysql_close($db);
?>
Now I can change it renders the td to include the individual columns, but I really want to add a little edit button on the right-hand side which will let me edit that particular row fields.
Any ideas?
Replace your while loop with the following code:
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
//create link for current node edit
echo "<td align='center'>". l(t('Edit this node'), 'node/' . $row['NodeId'] . '/edit') ."</td>";
echo "</tr>";
}
Remove echo "<br>"; and echo "<tr>"; below to while loop. This will resolve the issue for you

PHP echo without a loop. SQL Query

Here is the subject matter: Link, Click a players name to see.
I want to have the players name as a header so that the players name isn't repeated over and over again for each resulting row in the Roster table. I know how to loop through results for rows, but I only want the name once at the top and don't know how to do that. This is how I print the table code wise
<?php
$pid = $_POST['PID'];
$query = "SELECT Rosters.PID, Rosters.Goals, Rosters.Assists, Rosters.PIM, Rosters.Num, Rosters.TID, Players.pid, Players.firstname, Players.lastname,
(Rosters.Goals + Rosters.Assists) AS Points
FROM Rosters
INNER JOIN Players
ON Rosters.PID = Players.pid
WHERE Rosters.PID = $pid
ORDER BY Points DESC, Goals DESC";
$result = $conn->query($query);
$query2 = "SELECT Rosters.PID, SUM( Rosters.Goals ) Goals, SUM( Rosters.Assists ) Assists, SUM( Rosters.PIM ) PIM, Rosters.Num, Rosters.TID, Players.pid, Players.firstname, Players.lastname,
SUM((Rosters.Goals + Rosters.Assists)) AS Points
FROM Rosters
INNER JOIN Players ON Rosters.PID = Players.pid
WHERE Rosters.PID =$pid
ORDER BY Points DESC , Goals DESC";
$result2 = $conn->query($query2);
echo
if (!empty($_POST["PID"])) {
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Season</th>";
echo "<th class='hover'>Num</th>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
$i = 13;
$j = 14;
while ($row = $result->fetch_assoc()) {
$i++;
$j++;
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<tr><td>20" . $i . "-20" . $j . "</td>";
echo "<td>" . $row["Num"] . "</td>";
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
echo "<span class='style35'>Total</span><br />";
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
while ($row = $result2->fetch_assoc()) {
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
}
?>
I just want Players.firstname and Players.lastname printed once before any of the tables are generated.
Try this:
if (!empty($_POST["PID"])) {
$row = $result->fetch_assoc();
echo "<span class='style35'>" . $row["firstname"] . " " . $row["lastname"] . "</span><br />";
$result->data_seek(0);
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Season</th>";
echo "<th class='hover'>Num</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
$i = 13;
$j = 14;
while ($row = $result->fetch_assoc()) {
$i++;
$j++;
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<tr><td>20" . $i . "-20" . $j . "</td>";
echo "<td>" . $row["Num"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
echo "<span class='style35'>Total</span><br />";
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
while ($row = $result2->fetch_assoc()) {
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
}
?>
I don't know if you wanted the name of the player in the totals, so I left it there, but you can delete it if you want. I also applied to the player name the same style as the total.
You could get the first row of the result before your loop, then just reset the cursor, and completely ignore the players name inside your loop.
$first = $result->fetch_assoc();
// print out first name
echo($first['name'] . " " . $first['surname']);
// reset cursor
$result->data_seek(0);
// do your loops and stuff.
while ($row = $result->fetch_assoc()) {
...
}
You can do like this.If you do not want to change the sql query.
$ctr = 0;
while ($row = $result->fetch_assoc()) {
$ctr++;
if($ctr == 1) {
echo "</table>";
echo "<span class='style35'>Total</span><br />";
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
echo "<tr><td>" . $row["firstname"] . " " . $row["lastname"] . "</td> </tr>";
}
$i++;
$j++;
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<tr><td>20" . $i . "-20" . $j . "</td>";
echo "<td>" . $row["Num"] . "</td>";
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}

Undefined index for mysql result

<?php
$result = mysqli_query($conn,"SELECT * FROM news ORDER BY date DESC
LIMIT 5")or die(mysql_error());
if (!$result) {
printf("Error: %s\n", mysqli_error($conn));
exit();
}
echo "<table align='left' CELLPADDING='50' >";
while($row = mysqli_fetch_array($result))
{
if ($row['photoid'] == NULL){
$date_string = $row['date'];
$date = strtotime($date_string);
$date = date('m/d/y', $date);
echo "<tr>";
echo "<td style='width: 750px'>" .$row['title'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 700px'>" . $row['news'] . "</td>";
echo "<td style='width: 100px'>" . $date . "</td>";
echo "</tr>";
}
else
{
$result = mysqli_query($conn,"SELECT news.title,news.date,news.news,photo.photo FROM news, photo WHERE news.photoid = photo.photoid ORDER BY date DESC") or die(mysql_error());
$row = mysqli_fetch_array($result);
$date_string = $row['date'];
$date = strtotime($date_string);
$date = date('m/d/y', $date);
echo "<tr>";
echo "<td style='width: 750px'>" . $row['title'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width: 700px'>" . $row['news'] . "</td>";
echo "<td style='width: 100px'>" . $date . "</td>";
echo "</tr>";
echo "<td style='width: 800px'>" . '<img height="300" width="300" src="data:image/jpeg;base64,'.base64_encode( $row["photo"] ).'" >' . "</td>";
}
}
echo "</table>";
mysqli_close($conn);
?>
SO the website shows a news column, the code gets the news data from the database, if there is a photoid (if the news has a photo to go with it) then it adds the news to the table with a photo. If there is no photo is adds the news to the table without one. Simple. At the moment for testing I have two news articles both with photos, the first one works perfectly then the second errors
Undefined index: photoid in
For the line if ($row['photoid'] == NULL){. There is a photoid.
You need to check if variable/key is set before checking the value. Try this:
if (isset($row['photoid']) && $row['photoid'] == NULL){

Displaying information from a table then showing related information from another table

Hello I have 2 tables.
One is a table (nameinfo) that consists of contact information (account, name, email, address, city, state, zip, tags)
Table Two is an events tables (events) (email, event_type, event_name, date, keywords)
Here is what I am trying to do...
I would like to show a list of all the events that the person went to based on the matching email address. I would like to return the results like this...
nameinfo person 1
events person 1 went to
nameinfo person 2
events person 2 went to
nameinfo person 3
events person 3 went to
$query = mysql_query("SELECT * FROM nameinfo WHERE account='$a'");
while($row = mysql_fetch_array($query))
{
$i++;
echo "<div>";
echo "<table><tr style='font-weight:bold;color:green;font-size:9pt;text-align:left;'><td style='text-align:left;'
>";
echo "<button class='toggles' id='".$i."'/>"; echo $row['fullname']."</button>";
echo "</td>";
echo "<td >";
echo $row['target'];
echo "</td><td >";
echo $row['lname'];
echo "</td>";
echo "<td >";
echo $row['fname'];
echo "</td><td >";
echo $row['title'];
echo "</td>";
echo "<td >";
echo $row['company'];
echo "</td><td >";
echo $row['address'];
echo "</td>";
echo "<td >";
echo $row['address2'];
echo "</td><td>";
echo $row['city'];
echo "</td>";
echo "<td>";
echo $row['state'];
echo "</td><td>";
echo $row['zip'];
echo "</td>";
echo "<td>";
echo $row['email'];
echo "</td><td>";
echo $row['officenum'];
echo "</td>";
echo "<td>";
echo $row['cellnum'];
echo "</td>";
echo "<td>";
echo $row['date'];
echo "</td><td>";
echo $row['rep'];
echo "</td></tr></table></div>";
echo "<div style='width:100%;background:#000;color:#fff;display:none;' id='show-".$i."'>";
**echo THIS IS WHERE I WOULD LIKE TO PUT THE EVENT INFORMATION BUT HAVE NO IDEA HOW TO DO IT.;**
echo "</div>";
}
<script>
$("button.toggles").click(function() {
var value = $(this).attr('id');
$("#show-" + value).toggle();
});
</script>
Thank you for any assistance.
Run another query inside the loop. Here I am calling a function to do the job:
$i=0;
$link = ""\\your database connection
$query = mysql_query("SELECT * FROM nameinfo WHERE account='$a'");
while($row = mysql_fetch_array($query)){
$i++;
echo "<div>";
echo "<table><tr style='font-weight:bold;color:green;font-size:9pt;text-align:left;'><td style='text-align:left;'>";
echo "<button class='toggles' id='".$i."'/>"; echo $row['fullname']."</button>";
echo "</td>";
echo "<td >";
echo $row['target'];
echo "</td><td >";
echo $row['lname'];
echo "</td>";
echo "<td >";
echo $row['fname'];
echo "</td><td >";
echo $row['title'];
echo "</td>";
echo "<td >";
echo $row['company'];
echo "</td><td >";
echo $row['address'];
echo "</td>";
echo "<td >";
echo $row['address2'];
echo "</td><td>";
echo $row['city'];
echo "</td>";
echo "<td>";
echo $row['state'];
echo "</td><td>";
echo $row['zip'];
echo "</td>";
echo "<td>";
echo $row['email'];
echo "</td><td>";
echo $row['officenum'];
echo "</td>";
echo "<td>";
echo $row['cellnum'];
echo "</td>";
echo "<td>";
echo $row['date'];
echo "</td><td>";
echo $row['rep'];
echo "</td></tr></table></div>";
echo "<div style='width:100%;background:#000;color:#fff;display:none;' id='show-".$i."'>";
getEvent($row['email']);//Pass the email to the function here
echo "</div>";
}
function getEvent($email){
global $link;
$evant = "";
$query = mysql_query("SELECT * FROM events WHERE email='$email'", $link);
while($row = mysql_fetch_array($query)){
$event = $event . $row["event_name"] . "<br />";
//Format this however you wish to format
}
return $event;
}
<script>
$("button.toggles").click(function() {
var value = $(this).attr('id');
$("#show-" + value).toggle();
});
</script>

How can I join these two html tables together?

I want to create a compare sort of page. I currently have two tables with the information, but want it to be on table or join both together. The first row would show Name of brand, second would show cost, etc... I am grabbing the values of selected checkboxs and based on what they selected it get compared.
$testName = $_POST['testCompare'];
$rpl = str_replace("_", " ", $testName);
$firstOne = "$rpl[0]";
$secondOne = "$rpl[1]";
echo "$firstOne";
echo "<br/>";
echo "$secondOne";
$query = "SELECT * FROM test_table WHERE test_name = '$firstOne'";
$query2 = "SELECT * FROM test_table WHERE test_name = '$secondOne'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
$result2 = mysql_query($query2) or die ("Error in query: $query2. " . mysql_error());
if (mysql_num_rows($result) > 0 && mysql_num_rows($result2) > 0) {
//if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
if ($firstOne == $row[1]) {
{
echo "<table border=1>";
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<tr>";
echo "<td>" . $row[1] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[2] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[3] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row[4] . "</td>";
echo "</tr>";
}
}
}echo "</table>";
while($row2 = mysql_fetch_row($result2)) {
if ($secondOne == $row2[1]) {
{ echo "<table border=1>";
echo "<tr>";
echo "<td>" . $row2[0] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[1] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[2] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row2[3] . "</td>";
echo "</tr>";
echo "<td>" . $row2[4] . "</td>";
echo "</tr>";
}
}
}
echo "</table>";
}
else {
echo "No Results";
}[/CODE]
Thanks
Remove the </table> after the first loop.
Remove <table border=1> from both loops.
Add <table border=1 before the first loop.
Currently, you're defining a new <table border=1> each time you enter the loop. This will result in this HTML code:
<table ..>
<tr>...
<table ..>
..
<table>
..
Et cetera
</table>
<table ..>
Et cetera
</table>

Categories