How to give a href in a PHP while table - php

I've gotten into trouble while working on a school project, and I'm stuck.
I'm making a website where you can easily search for amusement parks.
I made a while loop with PHP and put information from the DB into the table which I looped. I've put all the information in a TD and I want to create a-href so when I click on one of the first parks it will redirect to an example: example.php?park=1 for the second park example: example.php?park=2.
Here is the while:
<div class="pretpark-container">
<table id="pretpark-table">
<?php while($pretpark = mysqli_fetch_array($result)){
echo "<tr>";
if(file_exists(__DIR__ . '/img/'. $pretpark['ParkImage'])): ?>
<td><img height="125px;" width="200px;"" src="/WikiParksWeb/Wikiparks-Website/img/<?php print($pretpark['ParkImage']); ?>"></td>
<?php else: ?>
<?php endif;
echo "<td>".$pretpark['ParkName']."</td>";
echo "<td>".$pretpark['ParkLocation']."</td>";
echo "<td>".$pretpark['ParkOpeninsTime']."</td>";
echo "<td>".$pretpark['ParkPrices']."</td>";
echo "<td>".$pretpark['ParkShortDescription']."</td>";
echo "</tr>";
?>
<tr class="filler"></tr>
<?php
}
?>
</table>
</div>
Each pretpark has an ID, the DB table of it is called ParkId.
Can someone help me where I have to put the a href. And how I can do it like its like this example: example.php?park= ParkId
I hope everything is understandable what I just said :/
Thanks,

You can do like this:
<div class="pretpark-container">
<table id="pretpark-table">
<?php while($pretpark = mysqli_fetch_array($result)){
echo "<tr>";
if(file_exists(__DIR__ . '/img/'. $pretpark['ParkImage'])): ?>
<td><img height="125px;" width="200px;"" src="/WikiParksWeb/Wikiparks-Website/img/<?php print($pretpark['ParkImage']); ?>"></td>
<?php else: ?>
<?php endif;
echo "<td>".$pretpark['ParkName']."</td>";
echo "<td>".$pretpark['ParkLocation']."</td>";
echo "<td>".$pretpark['ParkOpeninsTime']."</td>";
echo "<td>".$pretpark['ParkPrices']."</td>";
echo "<td>".$pretpark['ParkShortDescription']."</td>";
echo "<td><a href='example.php?park=".$pretpark['Parkid']."'> More Details</a></td>";
echo "</tr>";
?>
<tr class="filler"></tr>
<?php
}
?>
</table>
</div>

<div class="pretpark-container">
<table id="pretpark-table">
<?php while($pretpark = mysqli_fetch_array($result)){
echo "<tr>";
if(file_exists(__DIR__ . '/img/'. $pretpark['ParkImage'])): ?>
<td><img height="125px;" width="200px;"" src="/WikiParksWeb/Wikiparks-Website/img/<?php print($pretpark['ParkImage']); ?>"></td>
<?php else: ?>
<?php endif;
echo "<td>".$pretpark['ParkName']."</td>";
echo "<td>".$pretpark['ParkLocation']."</td>";
echo "<td>".$pretpark['ParkOpeninsTime']."</td>";
echo "<td>".$pretpark['ParkPrices']."</td>";
echo "<td>".$pretpark['ParkShortDescription']."</td>";
**echo "<td><a href='detailpage.php?id=".$pretpark['ParkId']."'></td>";**
echo "</tr>";
?>
<tr class="filler"></tr>
<?php
}
?>
</table>
</div>
I have added a line with bold formatting. You can get clue from this.

instead of the line
echo "<td>".$pretpark['ParkName']."</td>";
it would be something like..
echo "<td>".$pretpark['ParkName']."</td>";
backslashes are needed to escape the quotes so they come out in the HTML and are not interpreted by php

Related

show php variable in html heading [duplicate]

This question already has answers here:
php variable in html no other way than: <?php echo $var; ?>
(7 answers)
Closed 5 years ago.
i want to print a php variable in the html h1 tag. Important thing to know is that html is defined within the php as follow:
<?php
$test=" <html>
<h1><?php echo (rand(10,1000000)); ?></h1>
</html> ";
echo $test;
?>
You can write either 1:
<?php
$a="hi";
?>
<html>
<h1><?php echo $a;?> </h1>
</html>
or 2:
<?php
$a="hi";
echo "<html><h1>".$a."</h1></html>";
?>
use the following
<?php
$a="hi";
$prints =" <html>
<h1> $a</h1>
</html>";
echo"$prints";
?>
You can directly 'show' php variables by 'echoing' them in the html. For example consider this code.
<?php
include_once "./php/db.php";
$result=$conn->query("SELECT * FROM medtable");
while($row=mysqli_fetch_array($result)){
echo '<form id="form-'.$row["Product"].'"type="POST">';
echo '<table class="table table-hover table-condensed" id="table-'.$row["Product"].'">';
echo '<tr>';
echo'<td style="width:50%" name="name-'.$row["Product"].'">'.$row["Product"].'</td>';
echo '<td style="width:20%"> <input type="number" name="amount" id="num-'.$row["Product"].'" min="0" value="'.$row['Amount'].'"</input></td>';
echo '<td style="width:10%">'.$row["Price"]. '</td>';
echo '<td id="subtotal-'.$row["Product"].'"style="width:10%" value="'.$row["Subtotal"].'">'.$row["Subtotal"].'</td><input type="hidden" name="hidden-'.$row['Product'].'"id="hidden-'.$row["Product"].'"value="'.$row["Subtotal"].'"</input>';
echo '<td style="width:10%"><input type="button"id="'.$row["Product"].'"
name="'.$row["Product"].'"class="btn btn-primary" value="Submit">
</input></td>';
echo '<td><input type="hidden" name="product" value="'.$row["Product"].'"></input></td>';
echo "</tr>";
echo "</table></form>";
}
?>
As you can see a query is made from a table and html tags are then echod into the html file. Hope this helps :)

Make 4 columns in my while loop

Below is my while loop table, currently it's showing just one item per row. I can't seem to figure out how to get 4 items/columns to show before it shows a new row below. Any help with this would be much appreciated, thank you!
Jerome
<table style="background-color: white">
<?php
while ($row = mysql_fetch_array($rs)) {
?>
Collapse | Copy Code
<tr <?php //if ($i % 2) echo ' style="background-color: #ECECFB;"';?>>
<td class="rows"><font color="4D4D4D" size="3"><? echo '<a type="video/x-matroska" href=file://///'.$row["Link"].' target=blank>'.$row["Name"].'</a>' ?><br />
<? echo '<a type="video/x-matroska" href=file://///'.$row["Link"].' target=blank><img src='.$row["Picture"].' height=210px width=141px></a>' ?><br />
<? echo '('.$row["Type"].')' ?> <? echo $row["Year"] ?> <? echo $row["Rating"] ?> <? echo date('H:i', mktime(0,$row["Length"])); ?><br />
<? echo $row["Genre"] ?><br />
<? if ($row["Queue"]==='x') {
echo
"<center><a title=Remove From Watch List href='deletequeue.php?id=".$row['ID']."'><img align='center' width='20px' src='http://www.ourlittlelucas.net/ourflix/images/Minus.png'></a></td></font></center>";
}
else {
echo "<center><a title=Add To Watch List href='addqueue.php?id=".$row['ID']."'><img align='center' width='20px' src='http://www.ourlittlelucas.net/ourflix/images/Plus.png'></a></td></font></center>";
}
?>
</td></tr></font>
<?php $i++?>
<?php
}
?>
</table>
Try it
<table style="background-color: white">
<?php
while ($row = mysql_fetch_array($rs))
{
?>
Collapse | Copy Code
<tr <?php //if ($i % 2) echo ' style="background-color: #ECECFB;"';?>>
<td class="rows">
<font color="4D4D4D" size="3"><?php echo '<a type="video/x-matroska" href=file://///'.$row["Link"].' target=blank>'.$row["Name"].'</a>'; ?><br />
<?php echo '<a type="video/x-matroska" href=file://///'.$row["Link"].' target=blank><img src='.$row["Picture"].' height=210px width=141px></a>'; ?><br />
<?php echo '('.$row["Type"].')' ?>
<?php echo $row["Year"] ;?>
<?php echo $row["Rating"]; ?>
<?php echo date('H:i', mktime(0,$row["Length"])); ?><br />
<?php echo $row["Genre"] ;?><br />
<?php
if ($row["Queue"]=="x") {
echo
"<center><a title=Remove From Watch List href='deletequeue.php?id=".$row['ID']."'><img align='center' width='20px' src='http://www.ourlittlelucas.net/ourflix/images/Minus.png'></a></td></font></center>";
}
else
{
echo "<center><a title=Add To Watch List href='addqueue.php?id=".$row['ID']."'><img align='center' width='20px' src='http://www.ourlittlelucas.net/ourflix/images/Plus.png'></a></td></font></center>";
}
?>
</td></tr></font>
<?php $i++?>
<?php
}
?>
</table>

Call a function based upon an integer

I have a php table that displays the days of the month in a table:
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
echo "<td> $day_num </td>";
$day_num++;
$day_count++;
I have a second table that calls a separate query for each day of the month:
<tr>
<td> </td>
<td> </td>
<td><?php do { ?>
<?php echo $row_apr1['appt_time']; ?> <em><?php echo $row_apr1['nickname']; ?></em><br>
<?php } while ($row_apr1 = mysql_fetch_assoc($apr1)); ?></td>
<td><?php do { ?>
<?php echo $row_apr2['appt_time']; ?> <em><?php echo $row_apr2['nickname']; ?></em><br>
<?php } while ($row_apr2 = mysql_fetch_assoc($apr2)); ?></td>
<td><?php do { ?>
<?php echo $row_apr3['appt_time']; ?> <em><?php echo $row_apr3['nickname']; ?></em><br>
<?php } while ($row_apr3 = mysql_fetch_assoc($apr3)); ?></td>
<td><?php do { ?>
<?php echo $row_apr4['appt_time']; ?> <em><?php echo $row_apr4['nickname']; ?></em><br>
<?php } while ($row_apr4 = mysql_fetch_assoc($apr4)); ?></td>
<td><?php do { ?>
<?php echo $row_apr5['appt_time']; ?> <em><?php echo $row_apr5['nickname']; ?></em><br>
<?php } while ($row_apr5 = mysql_fetch_assoc($apr5)); ?></td>
</tr>
Is there a way I can merge the code together into one table, like using the $day_num value to call the query code?
Example: April 4th - $day_num would be = 4 and display a 4 in the table. But then can't I use php concatenation or something to call the code:
<br><?php echo $row_apr4['appt_time']; ?> <em><?php echo $row_apr4['nickname']; ?></em><br>
<?php } while ($row_apr4 = mysql_fetch_assoc($apr4)); ?>
Sorry if this has been done before a million times. I've been searching and can't seem to find what I'm looking for.
Thanks!
You can use the variable variables or $$ option. http://www.php.net/manual/en/language.variables.variable.php
<td>
<?php
$variable_name = 'apr' . $day_num;
do { ?>
<?php echo $row['appt_time']; ?>
<em><?php echo $row['nickname']; ?></em><br>
<?php } while ($row = mysql_fetch_assoc($$variable_name)); ?>
</td>
Not sure this is the best option for you but based off the code you have supplied it is a possible solution.
FYI you shouldn't use mysql_ functions as they are deprecated.

Displaying Reminders page from MySQL Database

I've created a PHP program for adding and viewing reminders. The add page works, but I'm having some trouble displaying them properly. How should I code this to only get the actual data? Also, how could I put this into an HTML table? (i.e. column for name, description, and date; rows are the retrieved data)
Thanks
When I open the file in my browser I get this as an output:
Array ( [reminderID] => 14 [reminderName] => Test [reminderDescript] => Test_Descript [reminderDate] => 2012 05 7 )
Code:
<?php include 'header.php'; ?>
<?php include 'database.php'; ?>
<div id="content">
<h1>Reminder List</h1>
<table align ="center">
<thead>
<tr>
<td>Name</td>
<td>Description</td>
<td>Date</td>
</tr>
</thead>
<?php
$query = 'SELECT * FROM reminder_event';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
print_r($row);
}
?>
</table>
<p><a href='reminder_add.php'>Add Reminder</a></p>
</div>
<?php include 'footer.php'; ?>
print_r() is a diagnostic tool for debugging, not to be used for real output. Instead, output HTML to a table using the array keys fetched from your row.
// Open a table
echo "<table>";
while($row = mysql_fetch_assoc($result)) {
// Output each row
echo "<tr>
<td>{$row['reminderName']}</td>
<td>{$row['reminderDescript']}</td>
<td>{$row['reminderDate']}</td>
</tr>";
}
// Close the table
echo "</table>";
Better still, escape each of the values for HTML output with [htmlspecialchars()] (http://us3.php.net/manual/en/function.htmlspecialchars.php) before output to prevent cross-site scripting attacks and broken HTML if characters like < > & are encountered in the values.
echo "<table>";
while($row = mysql_fetch_assoc($result)) {
// Encode all values for HTML output
$name = htmlspecialchars($row['reminderName']);
$desc = htmlspecialchars($row['reminderDescript']);
$date = htmlspecialchars($row['reminderDate']);
// Then output the encoded values
echo "<tr><td>$name</td><td>$desc</td><td>$date</td></tr>";
}
echo "</table>";
Change:
while($row = mysql_fetch_assoc($result)) {
print_r($row);
}
To:
while($row = mysql_fetch_assoc($result)) {
echo $row['reminderID'];
echo $row['reminderName'];
echo $row['reminderDescript'];
echo $row['reminderDate'];
}
You can echo those values out in whatever HTML you'd like. So, for example, if you want it in a table you would do something like this:
echo "<table><tr>";
while($row = mysql_fetch_assoc($result)) {
echo "<td>" . $row['reminderID'] . "</td>";
echo "<td>" . $row['reminderName'] . "</td>";
echo "<td>" . $row['reminderDescript'] . "</td>";
echo "<td>" . $row['reminderDate'] . "</td>";
}
echo "</tr></table>";
You can clean that up a bit to take some (or all) of the HTML out of the PHP.
<?php include 'header.php'; ?>
<?php include 'database.php'; ?>
<div id="content">
<h1>Reminder List</h1>
<table>
<thead><tr><td>id</td><td>name</td><td>description</td><td>date</td></tr></thead>
<tbody>
<?php
$query = 'SELECT * FROM reminder_event';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['reminderID']; ?></td>
<td><?php echo $row['reminderName']; ?></td>
<td><?php echo $row['reminderDescript']; ?></td>
<td><?php echo $row['reminderDate']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<p><a href='reminder_add.php'>Add Reminder</a></p>
</div>
<?php include 'footer.php'; ?>

how to organize the code?

<?php
if(arg(0)=="test"){
echo "code here";
}else{
echo '<div class="item-list">
<?php if (!empty($title)) : ?>
<h3><?php print $title; ?></h3>
<?php endif; ?>
<<?php print $options['type']; ?>>
<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes[$id]; ?>"><?php print $row; ?></li>
<?php endforeach; ?>
</<?php print $options['type']; ?>>
</div>
';}
the else echo is too long. is there a way to make it small.
Generally speaking, this is bad code. You are messing business logic with views. I would definitely use a very simple template system like :
Simple and Fast Template Engine
This code cannot be maintained and if it gets bigger, it would be a hell to even read. Please do yourself a favor and separate logic from views.
Take a look at this nice example from the url above, to see how code should be organized :
<?php
require_once('template.php');
$tpl = & new Template('./templates/');
$tpl->set('title', 'User Profile');
$profile = array(
'name' => 'Frank',
'email' => 'frank#bob.com',
'password' => 'ultra_secret'
);
$tpl->set_vars($profile);
echo $tpl->fetch('profile.tpl.php');
?>
The associated template looks like this:
<table cellpadding="3" border="0" cellspacing="1">
<tr>
<td>Name</td>
<td><?=$name;?></td>
</tr>
<tr>
<td>Email</td>
<td><?=$email;?></td>
</tr>
<tr>
<td>Password</td>
<td><?=$password;?></td>
</tr>
</table>
And the parsed output is as follows:
<table cellpadding="3" border="0" cellspacing="1">
<tr>
<td>Name</td>
<td>Frank</td>
</tr>
<tr>
<td>Email</td>
<td>frank#bob.com</td>
</tr>
<tr>
<td>Password</td>
<td>ultra_secret</td>
</tr>
</table>
I'm surprised that code would even run. Here's a better way, without using echo: just close the php interpreter while you generate your html output.
<?php
if(arg(0)=="test"){
echo "code here";
}else{
?>
<div class="item-list">
<?php if (!empty($title)) : ?>
<h3><?php print $title; ?></h3>
<?php endif; ?>
<<?php print $options['type']; ?>>
<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes[$id]; ?>"><?php print $row; ?></li>
<?php endforeach; ?>
</<?php print $options['type']; ?>>
</div>
<?
}
?>
<?php
$output = "<div class='item-list'>";
if(!empty($title)) $output .= "<h3>".$title."</h3>";
$output .= $options['type'];
foreach($rows as $id => $row) {
$output .= '<li class="' . $classes[$id] . '">' . $row . '</li>';
}
$ouput .= $options['type'] . "</div>";
$output = (arg(0) == "test" ? "code here" : $output);
echo $output;
?>
It looks nicer if you don't switch between PHP and HTML context (also indentation does help):
<?php
if (arg(0)=="test") {
echo "code here";
}
else {
echo '<div class="item-list">'
. #$title
. "<$options[type]>";
foreach ($rows as $id => $row) {
print "<li class=\"{$classes[$id]}\">$row</li>";
}
echo "</$options[type]>";
}

Categories