Calling a mysql query inside another mysql query while loop - php

include "mysql.php";
$query= "SELECT ID,name,displayname,established,summary,searchlink,imagename,image FROM institutions ORDER BY rand() ";
$result=mysql_query($query,$db);
while($row=mysql_fetch_array($result))
{
echo "<div class='grid-item item1' style='background: ".ran_col().";'>";
$query1= "SELECT content FROM ".$row['name']." limit 1";
$result1=mysql_query($query1,$db);
while($row1=mysql_fetch_array($result1))
{
echo "<div class='content-short' data-id=".$row['name'].">";
$string = $row1['content'];
if (strlen($string) > 200)
{
$trimstring = substr($string, 0,200). '...';
}
else
{
$trimstring = substr($string,0). '...';
}
echo $trimstring;
echo "</div>";
}
echo <"/div">;
}
What code should do:
The code should grab data from institution table and after that $row['name'] should be used as table for next mysql query where from that $row['name'] table $row['content'] should be grabbed
but it is not working as expeted

You are using same variable everywhere. Change them to
include "mysql.php";
$query= "SELECT ID,name,displayname,established,summary,searchlink,imagename,image FROM institutions ORDER BY rand() ";
$result=mysql_query($query,$db);
while($row=mysql_fetch_array($result))
{
echo "<div class='grid-item item1' style='background: ".ran_col().";'>";
$query1= "SELECT content FROM ".$row['name']." limit 1";
$result1=mysql_query($query1,$db);
while($row1=mysql_fetch_array($result1))
{
echo "<div class='content-short' data-id=".$row['name'].">";
$string = $row1['content'];
if (strlen($string) > 200)
{
$trimstring = substr($string, 0,200). '...';
}
else
{
$trimstring = substr($string,0). '...';
}
echo $trimstring;
echo "</div>";
}
echo <"/div">;
}
EDIT:
Also your query is wrong
$query1= "SELECT content FROM ".$row['name']" limit 1";
to
$query1= "SELECT content FROM ".$row['name']." limit 1";

Related

Sorting SQL table using PHP

I am having problems with sorting a table using PHP. I just want to figure out how to make buttons from which I can sort a table by criteria without damaging the code. The criteria are to sort three columns "Price", "ArtistName" and "AlbumName" in ascending and descending order. I am looking forward to opinions and I will be thankful for any tips.
<?php
$con = mysqli_connect("", "", "", "");
if (!$con) {
die("Error: " . mysqli_connect_error());
}
$mysqlget = "SELECT * FROM Albums";
$mysqldata = mysqli_query($con, $mysqlget) or die ("Error: " . mysql_error($con));
echo "<table class='table_albums'>";
echo "<tr><th>Album</th><th>Description</th><th>Artist</th><th>Price</th><th colspan='2'>Additional Options</th></tr>";
while ($row = mysqli_fetch_array($mysqldata, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo "<img class='album_artwork' src='";
echo $row['AlbumImage'];
echo "'></td><td><p class='info_album_artist'>";
echo $row['Info'];
echo "<p></td><td>";
echo "<img class='artist_artwork' src='";
echo $row['ArtistImage'];
echo "'></td><td><p class='info_album_artist'>£";
echo $row['Price'];
echo "<p></td><td>";
echo "<a href='";
echo $row['MoreInfo'];
echo "' class='more_info'>More Info</a></td><td>";
echo $row['Buy'];
echo "</td></tr>";
}
echo "</table>";
?>
There you go with all options
<form method="post" action="">
<select name="sort" onchange="this.form.submit()">
<option>Order by ...</option>
<option value="price_desc">Price descending</option>
<option value="price_asc">Price ascending</option>
<option value="artist_name_desc">Artist Name descending</option>
<option value="artist_name_asc">Artist Name ascending</option>
<option value="album_name_desc">Album Name descending</option>
<option value="album_name_asc">Album Name ascending</option>
</select>
</form>
<?php
$sort = $_POST['sort'];
if ($sort == 'price_desc')
{
$mysqlget = "SELECT * FROM Albums ORDER BY Price DESC";
}
else if ($sort == 'price_asc')
{
$mysqlget = "SELECT * FROM Albums ORDER BY Price ASC";
}
else if ($sort == 'artist_name_desc')
{
$mysqlget = "SELECT * FROM Albums ORDER BY ArtistName DESC";
}
else if ($sort == 'artist_name_asc')
{
$mysqlget = "SELECT * FROM Albums ORDER BY ArtistName ASC";
}
else if ($sort == 'album_name_desc')
{
$mysqlget = "SELECT * FROM Albums ORDER BY AlbumName DESC";
}
else if ($sort == 'album_name_asc')
{
$mysqlget = "SELECT * FROM Albums ORDER BY AlbumName ASC";
}
else
{
$mysqlget = "SELECT * FROM Albums";
}
$mysqldata = mysqli_query($con, $mysqlget) or die ("Error: " . mysql_error($con));
echo "<table class='table_albums'>";
echo "<tr><th>Album</th><th>Description</th><th>Artist</th><th>Price</th><th colspan='2'>Additional Options</th></tr>";
while ($row = mysqli_fetch_array($mysqldata, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo "<img class='album_artwork' src='";
echo $row['AlbumImage'];
echo "'></td><td><p class='info_album_artist'>";
echo $row['Info'];
echo "<p></td><td>";
echo "<img class='artist_artwork' src='";
echo $row['ArtistImage'];
echo "'></td><td><p class='info_album_artist'>£";
echo $row['Price'];
echo "<p></td><td>";
echo "<a href='";
echo $row['MoreInfo'];
echo "' class='more_info'>More Info</a></td><td>";
echo $row['Buy'];
echo "</td></tr>";
}
echo "</table>";
?>
That's easy using SQL. Example:
Sort by Info:
SELECT * FROM Albums ORDER BY Info
Sort in descending order:
SELECT * FROM Albums ORDER BY Info DESC
To make buttons that sort the table, I would use GET, like:
Info
PHP code:
$mysqlget = 'SElECT * FROM Albums';
if (isset($_GET['orderby'])) {
switch ($_GET['orderby']) {
case 'Info':
$mysqlget .= ' ORDER BY Info';
break;
case 'Artist':
$mysqlget .= ' ORDER BY Artist';
break;
// ...
}
}
$mysqldata = mysqli_query($con, $mysqlget) or die ("Error: " . mysql_error($con));

php code reuse. Is there a better way to do it?

I have upgrade my code. In the old code I had 2 functions: display_maker_success() and display_maker_fail() but I realised I can combine those two functions into one display_maker_stat() by putting more arguments into the function. I like it very much!
Is better way to do this? I want more code reuse.
function display_maker_success($link, $userid){
$status="closed";
$result="completed";
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 6;";
$result = mysql_query($sql, $link);
$isempty=mysql_num_rows($result);
If ($isempty ==0) {
echo "No Record";
} else {
echo "<table border=1>";
echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>Completed</td></tr>";
};
echo "</table>";
};
};
function display_maker_fail ($link, $userid) {
$status="closed";
$result="fail";
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
$result = mysql_query($sql, $link);
$isempty=mysql_num_rows($result);
If($isempty ==0){
echo "No Record";
} else {
echo "<table border=1>";
echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>fail</td></tr>";
};
echo "</table>";
};
};
function display_maker_stat ($link, $userid, $reuslt, $limit) {
$status="closed";
$result="fail";
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
$result = mysql_query($sql, $link);
$isempty=mysql_num_rows($result);
If($isempty ==0){
echo "No Record";
} else {
echo "<table border=1>";
echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$result</td></tr>";
};
echo "</table>";
};
};
Try the below,
Also there were few errors in your code and i have corrected them.
function display_maker_stat($link, $userid, $reuslt = 'fail', $limit)
{
$status = "closed";
$html = '';
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
$query = mysql_query($sql, $link);
if (mysql_num_rows($query) != 0) {
$html .= "<table border=1>";
$html .= "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($query, MYSQL_NUM)) {
$html.= "<tr><td>$row[0]</td><td>$row[1]</td><td>$result</td></tr>";
}
$html.= "</table>";
echo $html;
}
else {
echo "No Record";
}
}
Read about OOP

how can i give limit Repeat function in php/mysql

This is my script but i need limit in mysql result i used before LIMIT in mysql query but it's not worked.
function display_menus($parent_id = 0){
$i=0;
$sql=mysql_query("SELECT * FROM customer_dog_details_db WHERE dog_id_parent = '$parent_id' && username='root' && dog_id_filter='$_GET[dog_id]'");
if(mysql_num_rows($sql) > 0){
echo "<ul class='tree'>";
while ($row = mysql_fetch_array($sql)){
echo "<li><a href=''><span style='font-size:14px;'>". $row['dog_name'] ."</span>";
echo "<br> ". $row['dog_id'] ." <br>";
echo "". $row['sex'] ."<br>";
echo "".$row['whelped_date']."<br>";
echo "</a>";
display_menus($row['dog_id']);
echo "</li>";
}
echo "</ul>";
}
}
display_menus();
use:
$sql=mysql_query("SELECT * FROM customer_dog_details_db WHERE dog_id_parent = '$parent_id' AND username='root' AND dog_id_filter='$_GET[dog_id]' limit 0,1");
instead of:
$sql=mysql_query("SELECT * FROM customer_dog_details_db WHERE dog_id_parent = '$parent_id' && username='root' && dog_id_filter='$_GET[dog_id]'");

Single row repeating instead of displaying

I'm pretty new at PHP/MySQL, so please be patient with me.
I am trying to get a list of members in a table to show up on a page. Right now it's showing the first member about 10 times and not displaying anyone else's name. I DID have it working, but I don't know what happened. I just want it to display everyone's name once. Here is my code:
<?php $select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$row = mysql_fetch_array($select);
$rows = mysql_num_rows($select);
$teaching = $row[teaching];
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>
<?php }
$select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
$count = mysql_num_rows($select2);
$row2 = mysql_fetch_array($select2);
$student=$row2[student_name];
if($count==NULL) {
echo "<table width=\"80%\">\n";
echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";
}
else {
echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
echo "<table width=\"80%\" class=\"table-stripes\">\n";
echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
$select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
$row3 = mysql_fetch_array($select3);
while($row2 = mysql_fetch_array($select2)) {
$house=$row3[house];
echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
}
echo "</table>"; }
?>
I miss look on your code, since it is mess, but disregard the mysqli and mysql thing, you want to show how many student in the teacher's classes.
<?php $select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$row = mysql_fetch_array($select);
$rows = mysql_num_rows($select);
$teaching = $row[teaching]; <--- This only get first row of the course, if you want multiple course under same username, you need to loop it.
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>
<?php }
$select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
$count = mysql_num_rows($select2);
$row2 = mysql_fetch_array($select2);
$student=$row2[student_name]; <----- This only get the first row of the student name, if you want multiple student under a course, you need to loop it.
if($count==NULL) {
echo "<table width=\"80%\">\n";
echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";
}
else {
echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
echo "<table width=\"80%\" class=\"table-stripes\">\n";
echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
$select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
$row3 = mysql_fetch_array($select3);
while($row2 = mysql_fetch_array($select2)) {
$house=$row3[house]; <----This only show the first row of $house under same student, so you need to loop it too.
echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
}
echo "</table>"; }
?>
So what you really want to do is
<?php
$select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$rows = mysql_num_rows($select);
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>
<?php }
while( $row = mysqli_fetch_array( $select ) ) {
$teaching = $row[teaching];
$select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
$count = mysql_num_rows($select2);
if($count==NULL) {
echo "<table width=\"80%\">\n";
echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";
} else {
while( $row2 = mysql_fetch_array($select2) ) {
$student=$row2[student_name];
echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
echo "<table width=\"80%\" class=\"table-stripes\">\n";
echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
$select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
while($row3 = mysql_fetch_array($select3)) {
$house=$row3[house];
echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
}
echo "</table>";
}
} // END ELSE
}
} // END ELSE
?>

how to get id in mysql

this is showing all floors of building and i want to show selected building floors how can i do this i use this link floors.php?id=Building1 but its is not working please help me
if i write in there building1 it is working fine where buildingname='building1'
if i use this one where buildingname='$id' it is not working
how can i use like this
floors.php?id=Building1
if i enter this link then it will show all selected building result
<?php
$max_results = 8;
$from = (($page * $max_results) - $max_results);
if(empty($_POST)) {
$query = "SELECT * FROM floors where buildingname='$id' ORDER BY floorno ASC LIMIT $from, $max_results ";
}
$result = mysql_query("SET NAMES utf8"); //the main trick
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result);
$count=0;
while($row = mysql_fetch_array($result))
{
if($count%4==0)
{
echo "<tr/>";
echo "<tr>";
}
echo "<td><div align='center'><img src='images/floor.gif' width='60' height='90'></a><p>" . $row['floorno'] . "</p><div></td>";
$count++;
}
echo "</tr>";
echo "</table>";
echo '</div>';
?>
if(empty($_POST)) {
$query = "SELECT * FROM floors where buildingname='$id' ORDER BY floorno ASC LIMIT $from, $max_results ";
}
the right is:
if (isset($_GET['id'])) {
$id = filter_input(INPUT_GET, 'id');
$query = "SELECT * FROM floors where buildingname='$id' ORDER BY floorno ASC LIMIT $from, $max_results ";
}
that may help you.
<?php
$id =isset($_GET['id'])?$_GET['id']:null; // here you put value in $id only if it has some value.
$max_results = 8;
$from = (($page * $max_results) - $max_results);
if($id != null) {
$query = sprintf("SELECT * FROM floors WHERE buildingname='%s' ORDER BY floorno ASC LIMIT $from, $max_results", mysql_real_escape_string($id)); // safe from sql injection
$result = mysql_query("SET NAMES utf8"); //the main trick
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result);
$count=0;
while($row = mysql_fetch_array($result))
{
if($count%4==0)
{
echo "<tr/>";
echo "<tr>";
}
echo "<td><div align='center'><img src='images/floor.gif' width='60' height='90'></a><p>" . $row['floorno'] . "</p><div></td>";
$count++;
}
}
echo "</tr>";
echo "</table>";
echo '</div>';
?>

Categories