I'm learning PHP at the moment, started a practice project to make a todo list.
The list allows the user to enter data into the database with an input field, and uses a while and foreach loop to display the data from the database.
I want to add a check-box to each item displayed that allows the user to check what items on the list they'd like to remove, and for the check-box to have a value corresponding to the id column of the item, then I'll add a submit button that will clear the checked items.
The database table I'm using has two columns an auto increment id column, and a description column.
Here's the loop:
<?php
$query = "SELECT * FROM list_data;";
$list = $mysqli->query($query);
while ($row = $list->fetch_array(MYSQLI_ASSOC)):
echo "<tr>";
foreach($row as $list_item) {
echo "<td>" . $list_item . "</td>";
}
echo "</tr>";
endwhile;
?>
I tried this:
foreach ($row as $id => $description) {
echo "<td>" . $id . $description . "</td>";
}
But for soeme reason this returns the column name, and the values like so:
id1 descriptionTodo List Item Number One.
id2 descriptionTodo List Item Number Two.
id3 descriptionTodo List Item Number Three.
id5 descriptionTodo List Item Number Four.
Can anyone set me on the right path?
I've got the project uploaded onto github if anyone wants to see the whole lot.
Thanks in advance for any help and suggestions.
I don't understand why you are using foreach actually , I would just do this :
<?php
$query = "SELECT * FROM list_data;";
$list = $mysqli->query($query);
while ($row = $list->fetch_array(MYSQLI_ASSOC)):
echo "<tr>";
echo"<td><input type='checkbox' value='".$row['id']."'</td> <td>".$row['description']."</td>";
echo "</tr>";
endwhile;
?>
Related
Being a beginner in PHP, I have a table in a database which consists of 14 columns. I need to extract some of the columns through an 'href' link on another page. I am having a hard time trying to get the specific column ids for the specific column as I need the columns to be displayed as plain text separately on an html page.
So this is the fetch code to display columns 'A' and 'G' including two links in a table.
while($row=mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['A'] . "</td>";
echo "<td>" . $row['G'] . "</td>";
echo "<td>FASTA</td>";
echo "<td>Full Entry</td>";
echo "</tr>";
}
I am facing problems to get the columns A to M separately on the next php page of FullEntry.php and I need the data from the columns as plain text.
This is what I could come up with on FullEntry.php page.
$row = $_GET['rowid'];
<!--Here is where I need the multiple row ID's to get separate columnar data-->
echo $row;
?>
So How can use different id's for different columns from the original.php page to display results separately through the FullEntry.php page or If there can be any modifications with sql query as well. Help will be appreciated.
Any help would be appreciated.
Thank you in advance!
HereI have added a delimiter | bewteen $row reults like
echo "<td>Full Entry</td>";
And the result will be like Fullentry.php?rowid=a|b|c|d|e..... and you can access this by exploding the rowid.
$result = $_POST['rowid];
$result = explode("|",$result);
echo $result [0];
echo $result [1];...
I'm putting together something that's mean to allow for a user to book seats for a cinema showing. The row and seat numbers for every showing are stored in the database. I'm currently extracting them in the following method so that users can click on a seat button to select that seat for their booking:
echo "<form>";
echo "<table>";
while($row = mysqli_fetch_assoc($result)){
$rownum = $row['row'];
$seat = $row['seat'];
echo "<tr><td><button type=\"submit\" name=\"seatsel\" value=\"$rownum$seat\">$rownum$seat</button></td></tr>";
}
echo "</table>";
Right now this just outputs html showing all of the buttons as a single row in the table. I'd like the output to show seating across a single table row for every one of the table rows in the cinema screen. I'm not sure how to do this exactly given that each row is of differing lengths. E.g row A has twelve seats while row C has eight.
What would be the best way of accomplishing this?
You could easily update your code so that you will get a new table row every time the mysql row has another value. One thing to note is that you might want to add (depending on whether you're already sorting your results) the following ORDER BY row,seat.
echo "<form>";
echo "<table>";
echo "<tr>";
while($row = mysqli_fetch_assoc($result)){
if (!isset($oldrownumber)) $oldrownumber = $row['row'];
else if ($oldrownumber != $row['row']) {
echo "</tr><tr>";
$oldrownumber = $row['row'];
}
$rownum = $row['row'];
$seat = $row['seat'];
echo "<td><button type=\"submit\" name=\"seatsel\" value=\"$rownum$seat\">$rownum$seat</button></td>";
}
echo "</tr>";
echo "</table>";
I'm cobbling together a dropdown list that I would like to display the '[ve_venue]", "[ve_town]' from a MySQL query and, upon selection set a variable that I can use to pass the ve_ID on to an update query that adds a venue ID number to a separate table as a lookup key.
So far I've got some code that I've pieced together from various places and I can get it to display the town in a dropdown - I just need to add the venue field to the dropdown so I get "venue, town" in the list and I also need to be able to pass the ve_ID to a variable, say, $ve_ID so I can call it in some separate code (that will be on the same page in a separate include).
Here's what I've got so far....
<?
include('login_admin_sucess.php');
// Connects to your Database
include '../connect_include.php';
$query = "SELECT ve_ID, ve_venue, ve_town FROM `co_venues` ORDER BY ve_ID ";
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{
$ve_venue = $r['ve_venue'];
$result_array[$ve_venue][] = $r['ve_town'];
}
echo "<select name='x'>";
foreach($result_array as $v_venue => $value)
{
foreach($value as $title) {
echo "<option value='" . $v_venue . "'>" . $title . "</option>";
}
}
echo "</select>";
?>
I realise that mysql_ is deprecated...I'll work on that once I've got everything functioning.
You can concat the id and venue. For example:
$ve_venue = $r['ve_venue'] . "|" . $r['ve_ID'];
When you use the variable make a split with charapter "|" with preg_split function.
I want to print data from the database in a horizontal manner.
I have two table one that holds products names and another that holds products performance by months eg i want data to appear in a table like this
product name,performance by months from january to december
eg
product A,1000 ,2000, etc performance by months
product B,2000,3300, etc performace by months
Edit: I didn't realize you said you have two tables. So the query in my solution should be adapted with a JOIN and ordered, but we cannot dig further into this without knowing your schema. My solution addresses the main concern (i.e. printing results horizontally), provided you obtain two fields to show in two different rows.
Just retrieve your data and store it in a multidimensional array, THEN create the table.
$data = array();
$sql = "SELECT product, performance FROM table";
$rs = mysql_query($sql);
while ($row = mysql_fetch_assoc($rs))
{
$data[] = array($row['product'], $row['performance']);
}
echo "<table><tr>";
// print products in the first line of the table
foreach($data as $d)
{
echo "<td>" . $d[0] . "</td>";
}
echo "</tr><tr>";
// then print performances
foreach($data as $d)
{
echo "<td>" . $d[1] . "</td>";
}
echo "</tr></table>";
I have a problem here I can't solve. I have a database of houses with country, location, price etc entities. I print only the countries in a table using:
while ($data = mysql_fetch_array($select))
where $select is a query that selects specific data from the db. I want to display the full information of the selected db data on a different page i have created. The problem is that i don't know how to take the price of the data the user selected. When i get the price of the $_SESSION['counter'], its the number of the last entity of the db. . I don't want to use javascript etc. Here's the code of the page:
<?php
require_once 'php/core.php'; // opens the database
require_once 'php/openDB.php'; // starts the new session
$select = mysql_query("SELECT ID, Country, City FROM Houses");
$counter = 1;
echo "<form action='house_profile.php' method='get'>
<table width='400'>
<tr>
<td>No.</td>
<td>Country</td>
<td>City</td>";
echo "<tr>";
while ($data = mysql_fetch_array($select))
{
echo "<tr>";
echo "<td>" . $counter . "</td>";
echo "<td>" . $data['Country'] . "</td>";
// echo "<td>" . "<a href='house_profile.php' type='submit' value='$counter'>" . $data['City'] . "</a>" . "</td>";
echo "<td>" . $data['City'] . "</td>";
echo "<td><input type='submit' value='info' name='" . $counter . "'></td>";
echo "</tr>";
$_SESSION['counter'] = $counter;
$counter++;
}
echo "</table>
</form>";
?>`
Clarification:
You can pass any value to house_profile.php by adding a ? to start the querystring, then the name of the variable and the value. If you want to pass the id to house_profile then the link should look like this.
"<a href='house_profile.php?ID=" . $data['ID'] . "'>"
then in house_profile.php update your query like this
$select = mysql_query("SELECT * FROM Houses where ID = " . $_GET['ID']);
Now only the entity they clicked will be available.
FYI $counter is just the count, it's not necessarily the ID of the entity. e.g. if you delete ID 1 your records will start from ID 2 but counter will always start with 1. So to reference your entities correctly use the database ID.
Your query retrieves each house's ID from the database but doesn't display this (unless I don't understand your code). When the user chooses a house, you have to retrieve the ID and then use this in a separate query, such as
select price
from houses
where id = :ID
Currently you're displaying a line counter on your form as opposed to the house's ID - which explains why you write...
When i get the price of the $_SESSION['counter'], its the number of the last entity of the db.
You're passing the value of the row counter to the second query instead of the house's ID.