I am trying to form a string for the picture info in a lightbox gallery display. Originally I just used the following
<td><div style='height:175px;'></div></td><td><a href='images/memwork/".$cell1['folder']."/".$cell1['filename']."' data-lightbox='image-".$cell1['id']."' data-title='".$cell1['title']." by ".$cell1['artist']."<br />".$cell1['medium']." ".$cell1['width']." x ".$cell1['height']." cm. <br />Price: £".$cell1['priceu']."'><img class='tn' src='images/memwork/".$cell1['folder']."/".$cell1['tn']."' /></a></td>
But this became a nightmare trying to trap zero prices, dimensions or medai types with ternary operators so I am now trying to format the data with IF statements and add the result into the data array. However, I seem to have missed something as the result is always just " by ". My code is:
$query = "SELECT * FROM gallery WHERE sig=1 LIMIT $start,6";
}
$result = mysqli_query($db, $query);
$i=1;
while($row = mysqli_fetch_assoc($result)) {
${'cell'.$i} = $row;
$gal_artist = $row['artist'];
$query = "SELECT COUNT(*) AS num FROM gallery WHERE artist='$gal_artist'";
$total = mysqli_fetch_array(mysqli_query($db,$query));
${'cell'.$i}['num_pics'] = $total['num'];
$picdata = ${'cell'.Si}['title']." by ".${'cell'.Si}['artist'];
if (${'cell'.Si}['medium'].${'cell'.Si}['width'] !='') $picdata = $picdata."<br />";
if (${'cell'.Si}['width'] >0) $picdata = $picdata." ".${'cell'.Si}['medium']." ".${'cell'.Si}['width']." x ".${'cell'.Si}['height']." cm.";
if (${'cell'.Si}['price'] >0) $picdata = $picdata."<br />Price: £".${'cell'.Si}['price'];
${'cell'.Si}['picdata'] = $picdata;
$i++;
}
and the table data statements should simplify to
<td><div style='height:175px;'></div></td><td><a href='images/memwork/".$cell1['folder']."/".$cell1['filename']."' data-lightbox='image-".$cell1['id']."' data-title='".$cell1['picdata']."'><img class='tn' src='images/memwork/".$cell1['folder']."/".$cell1['tn']."' /></a></td>
You're using Si instead of $i in your code. In addition, I would suggest you to use array instead of dynamic variable name, which is usually a bad practice.
So :
$result = mysqli_query($db, $query);
$i=1;
while($row = mysqli_fetch_assoc($result)) {
$cells[$i] = $row;
$gal_artist = $row['artist'];
$query = "SELECT COUNT(*) AS num FROM gallery WHERE artist='$gal_artist'";
$total = mysqli_fetch_array(mysqli_query($db,$query));
$cells[$i]['num_pics'] = $total['num'];
$picdata = $cells[$i]['title']." by ".$cells[$i]['artist'];
if ($cells[$i]['medium'].$cells[$i]['width'] !='') $picdata = $picdata."<br />";
if ($cells[$i]['width'] >0) $picdata = $picdata." ".$cells[$i]['medium']." ".$cells[$i]['width']." x ".$cells[$i]['height']." cm.";
if ($cells[$i]['price'] >0) $picdata = $picdata."<br />Price: £".$cells[$i]['price'];
$cells[$i]['picdata'] = $picdata;
$i++;
}
Then print it like
<td><div style='height:175px;'></div></td><td><a href='images/memwork/".$cells[1]['folder']."/".$cells[1]['filename']."' data-lightbox='image-".$cells[1]['id']."' data-title='".$cells[1]['picdata']."'><img class='tn' src='images/memwork/".$cells[1]['folder']."/".$cells[1]['tn']."' /></a></td>
First thing you need to update your sql query by using below query.
SELECT t.*, (Select count(*) from test AS t1 where t.artist= t1.artist) AS album_count
FROM test AS t
I hope this will be helpful for you.
Related
/* To sort the id and limit the post by 40 */
$sql = "SELECT * FROM requests";
$result = $conn->query($sql);
$sqlall= "SELECT * FROM requests ";
$resultall = $conn->query($sqlall);
$i = 0;
if ($result->num_rows > 0) {
// Output data of each row
$idarray= array();
while($row = $result->fetch_assoc()) {
echo "<br>";
// Create an array to store the
// id of the blogs
array_push($idarray,$row['id']);
}
}
else {
echo "0 results";
}
?>
<?php
for($x = 1; $x < 40; $x++) {
// This is the loop to display all the stored blog posts
if(isset($x)) {
$query = mysqli_query(
$conn,"SELECT * FROM `requests`");
$res = mysqli_fetch_array($query);
$email1 = $res['email1'];
$msg1= $res['msg1'];
$subject1 = $res['subject1'];
$name1 = $res['name1'];
$id = $res['id'];
the output is 40 cards reading data from the first row in my database. can anyone help?
I'm using xampp.
This code is to show the loop, but if anyone wants the full code is here
You are storing all the IDs in the array $idarray, but then you don't really use them properly. You loop over them, but you just run SELECT * FROM requests` 40 more times, and always extract the same first row. You never use the ID to change the query.
But it really makes no sense to run lots of separate queries anyway. If you just want the first 40 rows then use MySQL's LIMIT keyword. It usually works best when combined with ORDER BY as well. Something like this:
$sql = "SELECT * FROM requests ORDER BY id LIMIT 40";
$result = $conn->query($sql);
while ($res = $result->fetch_assoc()) {
$email1 = $res['email1'];
$msg1 = $res['msg1'];
$subject1 = $res['subject1'];
$name1 = $res['name1'];
$id = $res['id'];
//example output, just for demo:
echo $email1." ".$msg1." ".$subject1." ".$name1." ".$id;
}
Documentation: https://dev.mysql.com/doc/refman/8.0/en/limit-optimization.html
I'm trying to get multiple values outside the while loop to use it in the query outside the while loop also. Now I can just get one value at a time. The values that I'm trying to get is this one $ids=$row["curriculum_id"];
This is my code, any help would be highly appreciated.
<?php
if(isset($_POST['Submit']) AND $_POST['Submit'] == 'Submit')
{
$coursneededdate =$_POST['needed'];
$coursneededdate =mysqli_real_escape_string($mysqli,$coursneededdate);
$gradunder=$_POST['gradunder'];
$gradunder =mysqli_real_escape_string ($mysqli,$gradunder);
$avalablesemster= $_POST['avalable'];
$avalablesemster =mysqli_real_escape_string($mysqli,$avalablesemster);
$sql = "SELECT * FROM curriculum where '".$coursneededdate."' between startdate and enddate";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$ids=$row["curriculum_id"];
}
}
}
$result2 ="SELECT * FROM curriculumcourses INNER JOIN courses ON curriculumcourses.course_id = courses.course_id where curriculum_id='$ids' and semester_ava='$avalablesemster'";
$result3 = $mysqli->query($result2);
if ($result3->num_rows > 0) {
while($row = $result3->fetch_assoc()) {
echo "<pre>";
print_r($row);
}
}
?>
This is a rough version (untested). This code was injectable, I've put in the mysqli_real_escape_string, http://php.net/manual/en/mysqli.real-escape-string.php. Consider using prepared statements in the future, How can I prevent SQL injection in PHP?.
<?php
if(isset($_POST['Submit']) AND $_POST['Submit'] == 'Submit') {
$coursneededdate = isset($_POST['needed']) ? $mysqli->real_escape_string($_POST['needed']) : '';
$gradunder=isset($_POST['gradunder']) ? $_POST['gradunder'] : ''; //this isn't used?
$avalablesemster= isset($_POST['avalable']) ? $mysqli->real_escape_string($_POST['avalable']) : '';
$sql = "SELECT * FROM curriculum where '".$coursneededdate."' between startdate and enddate";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
$ids = '';
while($row = $result->fetch_assoc()) {
$ids .= $mysqli->real_escape_string($row["curriculum_id"]) . ', ';
}
$ids = rtrim($ids, ', ');
}
}
$result2 ="SELECT * FROM curriculumcourses INNER JOIN courses ON curriculumcourses.course_id = courses.course_id where curriculum_id in($ids) and semester_ava='$avalablesemster'";
$result3 = $mysqli->query($result2);
if ($result3->num_rows > 0) {
while($row = $result3->fetch_assoc()) {
echo "<pre>";
print_r($row);
echo "</pre>";
}
}
?>
This code is concatenating all your curriculum ids into a comma delimited listed in the ids variable. This variable is then used with the SQL function in which will search for all ids in that list. Here's 2 docs on that topic https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in http://www.tutorialspoint.com/mysql/mysql-in-clause.htm.
You can replace both queries for just one:
SELECT * FROM curriculumcourses
NATURAL JOIN courses
WHERE semester_ava='$avalablesemster'
AND curriculum_id IN (
SELECT curriculum_id
FROM curriculum
WHERE '".$coursneededdate."' BETWEEN startdate AND enddate)
And then directly fetch the result from it.
This solutions is faster because the DBMS optimize the queries. Also, the option of query -> PHP -> query -> PHP it's more difficult to debug, read and maintain (in addition to be slower).
I am not sure how to do this. From the answer I've found on here, I think it's possible using jquery/ajax maybe. I need to get the value of my variable $i and echo it at the top, but it has to go through my logic to get to the value. Here is my code:
echo "<div id='records'><h1 align='center'>Today's Transfers (" . $i . ") </h1>
<table cellspacing='0' cellpadding='0'>
<tr>
<th>Customer Name</th><th>Phone Number</th><th>Disposition</th><th>User</th><th>Date Called</th>
</tr>
";
$i=0;
$sql = "SELECT * FROM vicidial_closer_log WHERE DATE(call_date) = DATE(NOW()) ORDER BY call_date DESC";
$result = mysqli_query($link, $sql, MYSQLI_STORE_RESULT);
while($row = $result->fetch_assoc()){
$i++;
$phone_number = $row['phone_number'];
$lead_id = $row['lead_id'];
$disposition = $row['status'];
...then echo those variable in my columns and rows...
}
echo "</table> </div>";
echo $i;
Maybe there is another way to write my code so the logic comes before I output everything? Not sure how to do that though since I'm using a mysql query.
You should get the number of rows from the result set and display those where the i is, since it is essentially the same thing.
$num_rows = mysql_num_rows($result);
Move your code above the echo statement:
$i=0;
$sql = "SELECT * FROM vicidial_closer_log WHERE DATE(call_date) = DATE(NOW()) ORDER BY call_date DESC";
$result = mysqli_query($link, $sql, MYSQLI_STORE_RESULT);
and add
$num_rows = mysql_num_rows($result);
echo "<div id='records'><h1 align='center'>Today's Transfers (" . $num_rows . ") </h1>
and use the rest of your code as before .. Might think about removing i if you don't need it for something else.
If $i is the total number of returned results, you could do:
echo mysqli_num_rows($result)
... and have your query executed at the top of the page.
This would ideally be all in a model and being set in a view by a controller however. That way you're separating the presentation from the functional logic.
I suggest you have a read about MVC:
Sure, you can just move the echo above your logic. But the best to do is to separate logic from presentation. You can create a class to handle this or maybe just another file, it depends on your needs.
$i=0;
$sql = "SELECT * FROM vicidial_closer_log WHERE DATE(call_date) = DATE(NOW()) ORDER BY call_date DESC";
$result = mysqli_query($link, $sql, MYSQLI_STORE_RESULT);
while($row = $result->fetch_assoc()){
$i++;
$phone_number = $row['phone_number'];
$lead_id = $row['lead_id'];
$disposition = $row['status'];
echo "<div id='records'><h1 align='center'>Today's Transfers (" . $i . ") </h1>
<table cellspacing='0' cellpadding='0'>
<tr>
<th>Customer Name</th><th>Phone Number</th><th>Disposition</th><th>User</th><th>Date Called</th>
</tr>";
Do your computations first, then display your data.
Horribly mangled example code:
$count = 0;
$output = '';
$res = mysqli_query("SELECT * FROM...");
while( $row = mysql_fetch($res) ) {
$output .= "<tr><td>..." . $row['somevar'] . "</td></tr>";
$count++;
}
echo "<h1>Yadda yadda $count</h1>";
echo "<table>";
echo $output
echo "</table>";
I am trying to retrieve the data stored in an unknown number of mySQL database rows and display them using HTML. At the moment I can display data from one row.
Each row has a unique id number, I was planning to iterate through by comparing this number to the variable counter. But it would then leave me with the issue of displaying the results in HTML. At the moment I am just echoing variables that contain data from the rows. However what I want to create is a HTML list that increases in length depending on how many rows are in the table.
Here is my current PHP code for retrieving a row from the database is:
$sql = "SELECT * FROM project_tasks WHERE project_name='$proj_name' AND task_id='$counter'";
$query = mysqli_query($db_conx, $sql);
$row = $query->fetch_assoc();
$task_id = $row["task_id"];
$proj_name = $row["project_name"];
$task_name = $row["task_name"];
$task_importance = $row["task_importance"];
$task_description = $row["task_description"];
$task_deadline = $row["task_deadline"];
$task_members = $row["task_members"];
$task_budget = $row["task_budget"];
At the moment I am just displaying some of the results in HTML using this code:
<div id="inner_container">
<?php echo "$task_id $proj_name $task_name $task_deadline"; ?>
</div>
$sql = "SELECT * FROM project_tasks WHERE project_name='$proj_name' AND task_id='$counter'";
$query = mysqli_query($db_conx, $sql);
while($row = $query->mysqli_fetch_assoc()) {
$task_id = $row["task_id"];
$proj_name = $row["project_name"];
$task_name = $row["task_name"];
$task_importance = $row["task_importance"];
$task_description = $row["task_description"];
$task_deadline = $row["task_deadline"];
$task_members = $row["task_members"];
$task_budget = $row["task_budget"];
echo "$task_id $proj_name $task_name $task_deadline";
}
Since you have built an associative array using fetch_assoc all you need to do is loop through that array. The OO example on http://php.net/manual/en/mysqli-result.fetch-assoc.php should get you what you need. A quick example:
$sql = "SELECT * FROM project_tasks WHERE project_name='$proj_name' AND task_id='$counter'";
$query = mysqli_query($db_conx, $sql);
echo '<div id="inner_container">';
while ($row = $query->fetch_assoc()) {
$proj_name = $row["project_name"];
$task_name = $row["task_name"];
$task_deadline = $row["task_deadline"];
echo "$task_id $proj_name $task_name $task_deadline";
};
/* free result set */
$row->free();
echo '</div>;
I'm a bit of a newb to PHP and MySQL. I seem to be having an issue with something. How do I loop through an array, querying each value in the array until the query meets a certain condition.. In this case it would be that the number of rows returned from the query is less than five. Here is what I have:
$query1="SELECT UserID FROM Users where RefID='$userid'";
$result1=mysql_query($query1);
while ($row = mysql_fetch_array($result1, MYSQL_NUM) && $sql2querynum < '5')
{
echo ($row[0]);
echo "
";
$sql2 = "SELECT * FROM Users WHERE RefID=$row[0]";
$sql2result = mysql_query($sql2);
$sql2querynum = mysql_numrows($sql2result);
}
Problem is, for every value it echoes out, I get the following warning:
mysql_numrows(): supplied argument is not a valid MySQL result resource
Like I said, I'm a newb, so maybe I'm not even going about doing this the right way.
try this
$query1="SELECT UserID FROM Users where RefID='$userid'";
$result1=mysql_query($query1);
if(mysql_num_rows($result1)<5)
{
while ($row = mysql_fetch_array($result1))
{
echo ($row[0]);
echo "
";
$sql2 = "SELECT * FROM Users WHERE RefID=$row[0]";
$sql2result = mysql_query($sql2);
$sql2querynum = mysql_numrows($sql2result);
}
}
$query1="SELECT UserID FROM Users where RefID='$userid'";
$result1=mysql_query($query1);
while ($row = mysql_fetch_array($result1, MYSQL_NUM) && $sql2querynum < '5')
{
echo ($row[0]);
echo "
";
$sql2 = "SELECT * FROM Users WHERE RefID={$row[0]}";
$sql2result = mysql_query($sql2);
$sql2querynum = mysql_numrows($sql2result);
}
Use { } for variables in " " ... and why you are not using joins ?