Get proper column ids of the sql query passed through a link - php

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];...

Related

Add check-box to remove from database in for each loop

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;
?>

Display data retrieved from MySQL in a rows / tabular form in PHP

I have written some data on MySQL database and would like it to be displayed on the webpage in the form of rows and coloumns.
echo "<table>";
echo "<tr>";
for($i=0; $i<$num_results; $i++) {
echo "<td style=\"width:30%\";>";
$row = $result->fetch_assoc();
echo htmlspecialchars(stripslashes($row['bagimage']));
echo "<img src=".htmlspecialchars(stripslashes($row['bagimage'])). " width=\"130px\" height=\"130px\" />";
echo "</strong><br/>Bag Type: ";
echo stripslashes($row['bagtype']);
echo "<br/>Price: ";
echo stripslashes($row['price']);
echo "<br/>Description: ";
echo stripslashes($row['description']);
echo "</p>";
echo "</td>";
}
echo "</tr>";
echo " </table>";
Now the data displayed here is all in just one row. I want it to display 4 in one row.
Like this
X X X X
X X X X
X X X X
The data consists of Image, price, description and type. which makes one cell of a table.
Now do we need a table here or can this be done in another way? Please help.
Also, The image code is kinda wrong as the image does not really display. Can someone please help me with the coding to be able to display the image correctly? should i be using stripslashes or mysql_real_escape_string instead? or is the entire code wrong?
note: 'bagimage' is the coloumn name for image which is of type longblob.
Thanks
I just got an answer after some research.
just added $results_per_row = 4;
and then inside the loop add this code:
if($i % $results_per_row == 0) {
echo "</tr><tr>"; }
you can also add line break instead of closing table row as per your code requirement.
However, I am still not able to figure out two things.
1) if the results are too many, more than say 100. How do display the results in more than one page or maybe the results will keep appearing as you scroll down using ajax maybe?
2) I am not able to get the images to display. I have tried everything possible.

How to display a single row from a database?

So I have been looking for ways to display data from a database. However, they all require a loop and I do not want a loop as I only have 1 row in this table.
I came across mysqli_fetch_row() but I am not sure how to implement this. I am starting to learn PHP and MySQL and so any help is appreciated! This is what I have so far...
$displayIntro = mysqli_query($connection,"SELECT * FROM Introduction");
$displayTitle = mysqli_fetch_row($displayIntro);
echo $displayTitle['Title'];
echo $displayTitle['Description'];
Also after displaying the plain text, how can I format it with HTML? For example, the title will need to be enclosed in <h1></h1> and the subscription in paragraph <p></p>.
Much thanks to any answers!
The problem is mysqli_fetch_row returns enumerated results, array with numeric indexes, so this should be like:
$displayIntro = mysqli_query($connection,"SELECT `Title`,`Description` FROM Introduction");
$displayTitle = mysqli_fetch_row($displayIntro);
echo $displayTitle[0]; // assuming column 'Title' is first row
echo $displayTitle[1]; // assuming column 'Description' is second row
What you should use here is mysqli_fetch_assoc to fetch a result row as an associative array:
$displayIntro = mysqli_query($connection,"SELECT `Title`,`Description` FROM Introduction");
$displayTitle = mysqli_fetch_assoc($displayIntro);
echo $displayTitle['Title'];
echo $displayTitle['Description'];
Use code from #Maximus2012 answer to form html row. Also to get only one row from table with more than one records you can just add LIMIT 1 at the end of the MySQL query like this:
"SELECT `Title`,`Description` FROM Introduction LIMIT 1"
Hope this helps :)
From PHP manual entry for mysqli_fetch_row (link):
"Fetches one row of data from the result set and returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero)." The function returns an enumerated array, not associative array.
Untested, but I would expect this to work:
echo $displayTitle[0];
echo $displayTitle[1];
$displayIntro = mysqli_query($connection,"SELECT * FROM Introduction");
$displayTitle = mysqli_fetch_row($displayIntro);
echo "<html>";
echo "<body>";
echo "<h1>" . $displayTitle['Title'] . "</h1>";
echo "<p>" . $displayTitle['Description'] . "</p>";
echo "</body>";
echo "</html>";

displaying last SQL database record on the top row in a html table in PHP

Hi below code prints results from first saved to last saved. Is there a way to get last saved on the top row once the data is displayed? (All records in the reverse order.) thanks.
while ($recz = mysql_fetch_array($runz))
{
echo "<tr class='taB'>";
echo "<td>".$recz["chkNum"]."</td>";
echo "<td>".$recz["InvoNum"]."</td>";
echo "<td>".$recz["InvoVal"]."</td>";
echo "<td>".$recz["InvoDate"]."</td>";
echo "<td>".$recz["type"]."</td>";
echo "<td class='ta'>".$recz["statu"]."</td>";
echo "</tr>";
}
You will need to change your query. Use ORDER BY ... DESC, where the ... is the column name that determines the order of the results.
In your HTML output, it looks like you have a column named InvoDate. Perhaps that's the column you need. If so, your query would end with ORDER BY InvoDate DESC.

Printing data horizontally using php and mysql

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>";

Categories