I would like to display SQL results, each line with html link, where each link contains the table id.
My table name : fruits
table_id content description price
---------------------------------------------------
1 apple tastes good 3 usd
2 peach grows on tree 4 usd
3 plump very purple 1 usd
It should display
Fruits
apple | tastes good
peach | grows on tree
plump | very purple
(Note: I haven't fetched price at this point)
My code so far:
$result = mysql_query("SELECT table_id,content,decription FROM fruits",$conn);
echo table
while ($row = mysql_fetch_row($res)) {
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "</tr>";
}
echo "</table>";
What I want to achieve instead of echo :
a href="link to fetch id 1">apple | tastes good
a href="link to fetch id 2">peach | grows on tree
a href="link to fetch id 3">plump | very purple
When i mean "link to fetch id 1", i mean like href="www.application.com/another.php and some way to pass fetch id 1 or 2 or 3 to another php file.
How can i make these links ?
How can I catch the passed id in another php file ?
Thank you for your help.
Something like
while ( false!=($row=mysql_fetch_assoc($res)) ) {
echo '
<tr>
<td><a href="details.php?id=', urlencode($row['table_id']), '">',
htmlspecialchars($row['content']),
'</td>
<td>', htmlspecialchars($row['decription']), '</td>
</tr>
';
}
you'd fetch the table_id in details.php from $_GET['id']. Test its existence first via isset()
For making the entire row "clickable" see Link entire table row?
Btw: the mysql_* extension is deprecated. Pick another API: mysqli or PDO_mysql.
see http://docs.php.net/manual/en/mysqlinfo.api.choosing.php
And while you're at it and since you most likely are going to use _GET['id'] in another query, take a look at prepared statements and (named) parameters.
Related
Hello and thanks for your time!
I'm trying to pull data from a mysql table using PHP, and am fairly new to both mysql and php.
I have a table built on racing information, where the following race results are recorded:
Date, Type, WL1, L1, L2, etc up to L15
WL1 column contains a value from L1 to L15, and stands for "winning lane 1" - basically what lane the car was in that came in first place.
Columns L1 to L15 contain racers' names.
Given the above, I'm trying to display the following in a table:
Date, Type, WL1 and the value of L1 through L15 as "Winner" when the data in columns L1 through L15 match the entry in column WL1.
Example:
-----------------------------------------
Date | Type | WL1| Winner
-----------------------------------------
May 5 | Rally | 13 | John
May 7 | Stock | 2 | Stewart
May 15 | Touring | 7 | Eddy
I'm able to achieve results like this using the following code:
$sql = "SELECT * FROM races";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Date</th>";
echo "<th>Type</th>";
echo "<th>WL1</th>";
echo "<th>Winner</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Type'] . "</td>";
echo "<td>" . $row['WL1'] . "</td>";
if($row['WL1'] == 'L1'){
echo "<td>" . $row['L1'] . "</td>";
}elseif($row['WL1'] == 'L2'){
echo "<td>" . $row['L2'] . "</td>";
That being said, I'm thinking there has to be an easier way than iterating through 15 values with if/elseif statements. I've tried both "while" and "for" loops with incremental values, but I only get empty results or errors, most likely due to my inexperience. I've also reproduced the above using a complex CASE statement in MySQL, but again, I'm trying to reduce lines of code.
If someone could suggest a more concise method of pulling this data, either through a MySQL statement or PHP code, I'd appreciate it. Even better if you can point to some good online examples/tutorials - the whole "teach a man to fish" thing...
Your data structure is what is causing your problem; you should really have a table that links racers and lanes for each race and then JOIN to that. Having said that, you can simplify your existing code by noting that the value in $row['WL1'] is the column name of the winner, so you can simply replace your if structure with:
echo "<td>" . $row[$row['WL1']] . "</td>";
I've created a sample data structure here with races, lanes and racers tables. To query from this structure you would do something like:
SELECT DATE_FORMAT(r.Date, '%b %e') AS Date, r.Type, r.Winner AS WL1, rs.Name AS Winner
FROM races r
JOIN lanes l ON l.Race = r.id AND l.Lane = r.Winner
JOIN racers rs ON rs.id = l.Racer
Which for my sample data gives:
Date Type WL1 Winner
May 5 Rally 13 John
May 7 Stock 2 Stewart
May 15 Touring 7 Eddy
I am currently using the following code to get data to display the way I want it to in the content section on my homepage:
$query = "SELECT distinct category FROM listings";
$result = mysql_query($query);
$catNo = 1;
echo "<table> <tr>";
while($row = mysql_fetch_array($result)){
echo "<td>" . $row['category'] . "</td>";
if ($catNo % 3 == 0) {
echo "</tr><tr>"
}
$catNo++;
}
echo "</tr> </table>";
Which results in this:
| CAT1 | CAT2 | CAT3 |
| CAT4 | CAT5 | CAT6 |
Is it possible for me to store a link in the database which means upon clicking on the cell i am brought to that href? Is it also possible to store an image in the database and display this in the cell with the name of the category?
Any help would be greatly appreciated.
Thanks.
Because you use distinct to get the results, it means for me that there is more rows with the same category in that table. If it's right, then you should create another table, called "listings_links" to store every link and image only once, then join this table to the listings table.
CREATE TABLE listings_links(
id INT AUTO_INCREMENT NOT NULL,
category CHAR(10) NOT NULL, //Use here the exact type you used in the listings table
link VARCHAR(200) NOT NULL,
image_path VARCHAR(200) NOT NULL,
PRIMARY KEY(id),
KEY(category));
Then use this:
<?php
$sql = "SELECT distinct listings.category, listings_links.link, listings_links.image_path FROM listings ";
$sql .= "LEFT JOIN listings_links on (listings.category=listings_links.category) "
$sql .= "ORDER BY 1;";
$result = mysql_query($sql) or die(mysql_error()."<br>".$sql);
$catNo = 1;
echo "<table> <tr>";
while($row = mysql_fetch_array($result)){
echo '<td><img src="'.$row['image_path'].'" border="0" />'.$row['category'].'</td>';
if ($catNo % 3 == 0) {
echo "</tr><tr>";
}
$catNo++;
}
echo "</tr> </table>";
?>
For the best performance (if you have a lot of categories) make an index on "category" in your listings table also.
UPDATE:
I also recommend to store the images in the file system instead of the database. This way the page will load faster (the browser can download more images in parallel) and you don't have to mess with the loading the images into the database (you should always do it when a category image is refreshed) and the code is more complicated too. But it can be done, but not recommended.
UPDATE 2:
You didn't ask for it, but if I were you, I don't store either the link nor the image path in the DB. I recommend to create a PHP that can show what you want by its parameter (for ex.: show_cat.php?cat1) and use this in the link and store all the images the way that I can direcly link to them by the category's name (for ex.: /images/categories/cat1.jpg, /images/categories/cat2.jpg etc.).
This way you don't have to alter your DB, and you only have to add the link and the image path to the loop. If you want, you can also check for the image file that it exists or not, and show a "No image available" pic instead.
Try something like this:
<?php
$query = "SELECT distinct category,link,image FROM listings";
$result = mysql_query($query);
$catNo = 1;
echo "<table> <tr>";
while($row = mysql_fetch_array($result)){
echo '<td><a href="'.$row['link'].'">'.$row['category'].'</td>';
echo '<td><img src ="/path/to/image/'.$row['image'].'"></td>';
if ($catNo % 3 == 0) {
echo "</tr><tr>";
}
$catNo++;
}
echo "</tr> </table>";
?>
I need to select multi-column information from a table and display it in another table which has just two columns. The scenario is this:
First Table(multi-columns):
MATH111 MATH112 MATH113 MATH114
67 89 54 23
I want to display it in this(two column table):
Course Score
MATH111 67
MATH112 89
MATH113 54
MATH114 23
I am conversant enough in php to obtain and display the information from the multicolumn table:
"select * from xx where id=''";
echo "<table border='1'>
<tr>
<th>MATH111</th>
<th>MATH112</th>
<th>MATH113</th>
<th>MATH114</th>
</tr>";
while($row=(mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['MATH111'] . "</td>";
echo "<td>" . $row['MATH112'] . "</td>";
echo "<td>" . $row['MATH113'] . "</td>";
echo "<td>" . $row['MATH114'] . "</td>";
echo "</tr>";
}
echo "</table>";
The table i want to display it in will be :
echo '<table><tr><th>Course</th><th>Score</th></tr>';
What i need is help on code to display it as i have outlined above, which is in this format:
Course Score
MATH111 67
MATH112 89
MATH113 54
MATH114 23
Please let me add that the values for the "course" column
is based on the table being selected.So i can not hard code it.
re-write your SQL query to something like this
select 'MATH111' Course, MATH111 Score from xx where id=something
union
select 'MATH112' Course, MATH112 Score from xx where id=something
union
select 'MATH113' Course, MATH113 Score from xx where id=something
union
select 'MATH114' Course, MATH114 Score from xx where id=something
and that should take care of the rest
Why not just:
echo '<tr><td>MATH111</td><td>', $row['MATH111'], '</td></tr>',
'<tr><td>MATH112</td><td>', $row['MATH112'], '</td></tr>' [ ... ];
I'm now hopeless about this problem and that's why I'm here. I'm kind of a starter in PHP and mysql programming. I searched for a solution on the web but I wasn't succeeded. I'm working on a project what is a car repairing administration system. This is a part of it that I need.
I have two tables:
repairs (j_id /this is the primary key/, rendszam, javitas, megjegyzes, datum)
and
pictures (kep_id /this is the primary key/, j_id /it's from repairs j_id/, kepnev)
I need to display a result in a table, where I can see that which of the repairs have a picture in the pictures table, but without redundancy, so I don't want multiple repair rows that are similar to eachother, but one repair row with multiple picture columns after it.
What I have done already:
<?php
$sql = "SELECT $tbl_name2.j_id, $tbl_name2.rendszam, $tbl_name2.javitas, $tbl_name2.megjegyzes, $tbl_name2.datum, $tbl_name3.kepnev FROM $tbl_name2, $tbl_name3 WHERE $tbl_name2.j_id=$tbl_name3.j_id";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>" . $row[$i] . "</td>";
echo "<td>" . $row[$i + 1] . "</td>";
echo "<td>" . $row[$i + 2] . "</td>";
echo "<td>" . $row[$i + 3] . "</td>";
echo "<td>" . $row[$i + 4] . "</td>";
echo "<td>" . $row[$i + 5] . "</td>";
}
?>
This one displays information like this:
repair id | picture
1 | fdgdfg.jpg
1 | fgdfg.jpg
1 | fghh.jpg
25 | dfg.jpg
25 | jkjk.jpg
But I don't want to have multiple repair rows but one repair row with multiple pictures after it.
I tried this:
$sql2="SELECT DISTINCT $tbl_name2.j_id, $tbl_name3.kepnev FROM $tbl_name2, $tbl_name3 WHERE $tbl_name2.j_id=$tbl_name3.j_id";
$result2=mysql_query($sql2) or die(mysql_error());
$sql="SELECT $tbl_name2.j_id, $tbl_name2.rendszam, $tbl_name2.javitas, $tbl_name2.megjegyzes, $tbl_name2.datum, $tbl_name3.kepnev FROM $tbl_name2, $tbl_name3 WHERE $tbl_name2.j_id=$tbl_name3.j_id GROUP BY j_id;
$result=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($result))
{
echo "<tr><td>".$row[$i]."</td>";
echo "<td>".$row[$i+1]."</td>";
echo "<td>".$row[$i+2]."</td>";
echo "<td>".$row[$i+3]."</td>";
echo "<td>".$row[$i+4]."</td>";
echo "<td>".$row[$i+5]."</td>";
while($sor=mysql_fetch_array($result2))
{
if($sor['j_id']==$row[$i]){
echo "<td><a href=kepek/".$sor['kepnev']." target=_blank>".$sor['kepnev']."</a></td>";
}
}
}
The sql statement differs in the group by in $sql, but this one displays the results like this:
repair id | picture
1 | fdgdfg.jpg | fgdfg.jpg | fghh.jpg
25 |
So it's just not continuing after the first repair id, not showing the repair pictures for repair id 25.
I don't know, how can I get it right. I need a table which displays information like this:
repair id | picture
1 | fdgdfg.jpg | fgdfg.jpg | fghh.jpg
25 | dfg.jpg | jkjk.jpg
Could you help me out in this?
This sounds fairly straightforward, looks like you're off to a good start.
I've done something similar in the past, and I just used a variable to keep track of the current ID being displayed and the previously displayed ID. Here's a quick code sample to outline what I mean by that.
while($row=mysql_fetch_array($result))
{
$currentID=$row[$i];
if ($currentID != $lastID) {
echo "</tr><tr><td>".$row[$i]."</td>";
} else {
echo "<td>".$row[$i]."</td>";
}
$lastID=$row[$i];
}
So, the idea is to keep track of what the last ID was. In the next row, if the ID is the same, just output <td>"variable goes here"</td> to keep it on the same column. If the ID changed, end the row and create a new row using </tr><tr>". Feel free to comment if you need more help.
I am using AJAX Pagination for displaying my records in application from MySQL using PHP.
I have done that successfully but just need one solution:
I need to display a Serial No while displaying records like:
----------------------------
S.No Name Country
----------------------------
1 Sandeep India
2 Rahul Japan
3 Riya China
4 Sohan India
...
50 James USA
If I have set to show 20 results per page while paginating the first page shows serial no. 1 to 20 , 2nd page shows serial no 1 to 20 and 3rd page shows serial no 1 to 10.
But I want to show serial no. 1 - 20 in first page , 21- 40 in second page , 41- 50 in 3rd page.
How can I do this in PHP?
Here is my code:
$ssql = $ssql_select.$ssql_where." order by reg_date desc LIMIT $Page_Start , $Per_Page";
$rs=mysql_query($ssql, $cn) or die("MySQL error: ".mysql_error());
$ctr = 1;
while ($row = mysql_fetch_object($rs)) {
echo "<td align=center>".$ctr."</td>";
echo "<td align=center>".$row->name."</td>";
echo "<td align=center>".$row->country."</td>";
$ctr++;
}
Try
...
while ($row = mysql_fetch_object($rs)) {
echo "<td align=center>" . $ctr + $Page_Start . "</td>";
echo "<td align=center>".$row->name."</td>";
echo "<td align=center>".$row->country."</td>";
$ctr++;
}
...