I've written a PHP script that can populate a table in a particular way so that multiple events (or no events) can be put in one square in an HTML - similar to the layout a calendar would have. But, there's a problem, the while statement I created to fill in squares in the table when there is no data doesn't detect when there is data, and fills the entire table with empty squares. This is what the output looks like (The page is styled using Bootstrap 3). From the mysql data I have provided, these events should be in the square at {Period 1, Monday}.
Here is my data in a mysql database; mysql data
Here is a snippet of the part of the page related to this table;
<?php
$query = "SELECT * FROM configtimetabletwo WHERE term = ".$term." AND week = ".$week." ORDER BY period, day LIMIT 100;";
$results = mysqli_query($conn, $query);
$pp=1; //The current y value of the table
$pd=0; //The current x value of the table
echo '<tr><td>';
while($row = mysqli_fetch_row($results)) {
while((pd!=$row[3] or $pp!=$row[4]) and $pp<6){ //This while statement fills in empty squares and numbers each row.
if($pd==0) {
echo $pp."</td><td>";
$pd++;
}
elseif($pd<5){
echo "</td><td>";
$pd++;
}
else {
echo "</td></tr><tr><td>";
$pd=0;
$pp++;
}
}
echo '<a href="?edit='.$row[0].'" class="label label-default">';
echo $row[5].' '.$row[6].' - '.$row[7]."</a><br>";
}
echo "</td></tr></table>"
?>
I haven't been able to figure out why this happens so far, thanks in advance to anyone who has any idea what's going on.
In the comments below my question, pavlovich pointed out the error. In this case, it was simply an issue of forgetting to use a $ to reference a variable. It would seem that this doesn't throw an error in a while statement like it would elsewhere.
Related
I wanna build a presence check for our choir in the style of tinder but not as complex.
The database contains names and file paths of pictures of the members. When you click on the "present" or "not present" button, the next picture and name should be shown. In the background, the database table should be updated with true/false for presence. (this will be done later)
My problem is that it almost works, but instead of showing one member, it shows all members with their pictures in one single page.
I understand that I could fire with Javascript to continue and paused php-function but I don't get the clue how.
I tried "break" in the php and call the function again but that didn't work.
<?php
$conn = new mysqli(myServer, myUser, myPass, myDbName);
$sql = "SELECT * FROM mitglieder";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<img class='pic' src='" .$row["folder"]. "/" .$row["img"]. "'><br>" ;
echo "<div id='name'>" .$row["vorname"]. " " .$row["name"]. "</div> <br>";
echo "<img src=''img.png' id='present' onclick='isPresent()'>";
}
} else {
echo "0 results";
}
$conn->close();
?>
<html>
<script>
$( document ).ready(function() {
console.log("Ready");
);
</html>`
You can use php function
mysqli_fetch_all()
assign it on the variable outside the while loop and loop or access the indexes in your code.
For Example:
$data = mysqli_fetch_all();
echo $data[0]['name'];
foreach($data as $item)
{
echo $item['name'];
}
You need a way to establish a "state" between your web page and the PHP backend so that you can step through the data. I suggest something like this:
Use an auto-increment integer primary key for the database. That way you can access the data in index order. Let's name the column id
Have your JS code send a form variable - named something like LAST_ID to the PHP in your get. i.e http://someurl.com/script.php?LAST_ID=0
On your first call to the server, send LAST_ID = 0
In your PHP code, fetch the value like this: $id = $_GET('LAST_ID');
Change your SQL query to use the value to fetch the next member like this:
$sql = sprintf("SELECT * FROM mitglieder where id > %d limit 1", $id); That will get the next member from the DB and return only 1 row (or nothing at the end of data).
Make sure to return the id as part of the form data back to the page and then set LAST_ID to that value on the next call.
You can use a HTTP POST with a form variable to the server call that sets that member id to present (maybe a different script or added to your same PHP script with a test for POST vs GET). I suggest a child table for that indexed on id and date.
I hope that puts you in a good direction. Good luck
Having solved the populating the dropdown from the array, the next phase of my project is pulling data from a database to allow users to put limits on the data they want to see.
Again, I'm having trouble populating the dropdown. This time however, the problem seems to be that when I put the HTML interspersed with the PHP, the whole thing stops- the form loads, but nothing goes into the drop down.
Here's the code.
//Step One: Query the DB, and get a list of monthly reports
$sql = "SELECT Report_Text, Report_Date FROM ADM_8_Reports_List";
$result = $conn->query($sql);
//print_r ($result);
$reports_in_db = mysqli_fetch_all($result,MYSQLI_ASSOC);
if (sizeof($reports_in_db) > 0)
{
print_r ($reports_in_db);
echo "\n";
//Now, like the front page, create a form and populate it.
?>
Choose a Monthly Report <select name = "monthly_report">
<?php
foreach($reports_in_db as $row)
{
echo 'option value"' . $row ["Report_Date"] . '">' . $$row ["Report_Date"] . " " . $row ["Report_Text"] . '</option>';
}
}
else
{
echo "I'm sorry, there's no data here. Please start again\n";
} ?>
</select>
<input type="submit" name="monthly report" value="Retrieve Report"">
the output
what it looks like is happening is that the code is freezing where I'm interpolating the HTML and PHP, and actually creating the dropdown.
basically on the previous page, the user can choose one of 4 things to do. On this page, there's an if statement based on $_POST from the previous page. Depending on the choice, the databased will be polled to get, in this case, a list of all the monthly reports. I've copied exactly the structure of what I did on the first page (where I was going from an associative array I hard-defined). I can't figure out what's wrong with the declaration of the dropdown, but that's where the thing's .. well stopping.
I am really annoyed by this.
I forgot '<'when declaring the option. And didn't see it until I posted the question here.
Sorry.
A PHP page must show the content of a database's table. In another table, there is the value I will use to print the row accordingly and show as a child.
$result = pg_query($conn, "SELECT *,'SENT' as direction FROM table1 WHERE identification LIKE '%$identification%' AND expedition LIKE '$expedition' order by date DESC LIMIT '$limit' ");
This, inside a WHILE, will print this kind of table:
while ($row = pg_fetch_row($result)) {
$sql2 = pg_query($conn, " SELECT quantity FROM expedition WHERE identification='$row[2]' ");
$row2 = pg_fetch_row($sql2);
$color="white";
if ($row2[0] == "-1") { $color="red";}
if ($row2[0] == "1") { $color="green";}
if ($row2[0] == "0") { $color="grey";}
echo "<tr bgcolor=\"$color\">
<td><button class=\"btn btn-info\" type=\"button\" data-toggle=\"collapse\" data-target=\"#$row[2]\">+</button></td>
<td>row[0]</td>
<td>row[1]</td>
<td>row[2]</td>
<td>row[3]</td>
<td>row[4]</td>
<td>...and more</td>
</tr>";
echo "<tr><td colspan=9><div style=\"background-color:$color\" id=\"$row[2]\" class=\"collapse\">$row2[0]</div></td></tr> ";
I am using bootstrap to add a button that, once clicked, will open a hidden row with the content of $sql2. Query that I will print straight after the </tr>.
Notice the 1st query as row[] and the second query as row2[].
The problem:
If the second query contains more than 1 result and in all cases identification is not a PK (therefore there could be more), I am not able to print for each row the colour of its childs.
I need to extend the second query to manage a WHILE loop, in order to extract, through identification, all the values of its parent.
IF, sql2 contains -1 or 1 or 0, the parent and itself must be painted accordingly.
IF, sql2 contains different values, both of them must be painted red (as error).
The obvious choice would be to move the SQL2 at the end of the first WHILE, in this case I can have SQL2 with all the records I want.
just like using arrays or the FOR structure.
Stays though the fact that moving the code would not allow me to print all the rows, because html is wrote already.
In this case I believe I'd need Jquery to highlight the values once they are written already.
I do not know a thing of Jquery and I'd appreciate any suggestion or help.
Attached a image to show what I achieved, the green bar only contain 1 child as the code above. Forgive the ugly interface!
I have 2 questions. One is if there is any error in this code.
The 2nd question I want to ask is how do you know which <li> item is selected. Right now, the code performs a search in mySQL for all rows that matches city, language and level and returns the results in a list item.
I want it so that when the user clicks on anyone of the list items, it will goes into another page displaying a more detail description by querying the selected list item.
I have a guess, which is for step 2, I also grab the ID (primary key) for each row and somehow keep that stored within the list but not echo.. Would I need to wrap <a> in <form action="XX.php" method="get">?
<?php
//1. Define variables
$find_language = $_GET['find_language'];
$find_level = $_GET['find_level'];
$find_city = $_GET['find_city'];
//2. Perform database query
$results = mysql_query("
SELECT name, city, language, level, language_learn, learn_level FROM user
WHERE city='{$find_city}' && language='{$find_language}' && level='{$find_level}'", $connection)
or die("Database query failed: ". mysql_error());
//3. Use returned data
while ($row = mysql_fetch_array($results)){
echo "<li>";
echo "<a href=\"#result_detail\" data-transition=\"flow\">";
echo "<h3>".$row["name"]."</h3>";
echo "<p>Lives in ".$row["city"]."</p>";
echo "<p>Knows ".$row["level"]." ".$row["language"]."</p>";
echo "<p>Wants to learn ".$row["learn_level"]." ".$row["language_learn"]."</p>";
echo "</a>";
echo "</li>";}
?>
Your code looks alright from what I can see. Does it not work?
One way would be to get the ID through your SQL-query as well and then add the id to the href in your link. Then you can fetch it through the querystring on the other page, to display the proper post depending on which li-element the user clicked on:
echo "<a href=\"details.php?id=". $row["id"] ."\" data-transition=\"flow\">";
Not sure how you mean "somehow keep that stored within the list but not echo" - it wouldn't be possible to store anything in the list, if it is not echoed, as it then wouldn't be sent to the client. You can of course store the id in a data-attribute on the li-element, which won't be displayed to the user. It will however be visible through the source code! Don't know why that should be a problem though?
I am making a page where people can make posts. All of those posts are then shown in a table of 24 cells. I can have the last 24 posts shown with no problem, but now I don't know how to show the prior group(s) of posts. How can I fix my code to do that? I actually have this:
(I'm removing lines to make it easy to read)
$sql = "SELECT
topics.topic_id,
topics.topic_subject
ORDER BY
topics.topic_id DESC";
// ---check everything is fine---- //
function retrieve_info($result)
{
if($row = mysql_fetch_assoc($result))
{echo $topic_if; echo $topic_subject; //and what I want in every cell
}
}
<table width="100%" height="751" >
<tr><td><?php retrieve_info($result);?></td>
<td><?php retrieve_info($result);?></td>
<td><?php retrieve_info($result);?></td>
<td><?php retrieve_info($result);?></td></tr>
<!-- repeat a few more times :-) -->
</table>
I though that by changing the variable $row with a number before the if statement would alter the output, but I still see the same data printed on screen. What should I do to be able to show next group of posts?
Thanks!!!
At some point when you have hundreds or thousands of records, you are going to want to paginate the results and not just select all records from the table.
To do this you will run one query per 24 records, your sql would be more like this:
$sql = "SELECT
topics.topic_id,
topics.topic_subject
ORDER BY
topics.topic_id DESC
LIMIT 0, 24
";
and for the next 24,
LIMIT 24, 24
then
LIMIT 48, 24
and so on.
You would then make next/previous buttons to click which would refresh the page and dispay the next 24, or you would get the next results with an AJAX request and append the next 24 through the DOM.
This suggests having to take a slightly different approach then calling the same function from each table cell.
More like get the relevant 24 results based on the page number you are on, then loop through the results array and print out the table code with values inside it. Based on if the iterator of the loop is divisible by 4 (looks like your grid is 4x6), you print out new tags for the new row, and that sort of thing.
Search around a bit for pagination in php and mysql to get a sense of how this all fits together.
function retrieve_info($result)
{
while($row = mysql_fetch_assoc($result))
{
$topic_id = htmlspecialchars($row['topic_id']);
$topic_subject = htmlspecialchars($row['topic_subject']);
echo '<td>';
echo $topic_if;
echo $topic_subject; //and what I want in every cell
echo '</td>';
}
}