This is a part of my php source code.
I don't know how to put variation inside to echo function.
<?php
$putMeInside = "please";
?>
<?php
while($row = $result->fetch_assoc()){
echo
"<tr>
<div onClick=\"saveToDatabase(this,'name','777')\">{$row['name']}</div>
</tr>\n";
}
?>
How can I $putMeInside value to the place of '777' (parameter of saveToDatabase function) ?
Too simple question to some people, but I don't know it really. I tried over 15 times edit on and on, But couldn't find result. please help me...
try this
echo "
<tr>
<div onClick="saveToDatabase(this,'name','$putMeInside')\">
{
$row['name']
}
</div>
</tr>\n";
<?php
while($row = $result->fetch_assoc()){
echo
"<tr>
<div onClick=\"saveToDatabase(this,'name','" . $putMeInside . "')\">{$row['name']}</div>
</tr>\n";
}
?>
echo
"<tr>
<div onClick=\"saveToDatabase(this,'name','$putMeInside')\">{$row['name']}</div>
</tr>\n";
Related
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
I'm sorry if this is unclear or difficult to understand, but explaining what I am attempting to do isn't that easy over text.
In the HTML code like this entry works:
<th>Name</th>
<td><?php echo $row["name"];?></td>
but in PHP I can not advise:
echo "<th>Name</th>";
echo "<td><?php echo $row["name"];?></td>";
"id" has value /name, lastname, city, district/
"action" has value /ASC, DESC/
Complete table for clarity I not want to indicate.
Can you help me?
Now I have:
echo "<th>Name</th>";
echo "<td><?php echo '".$row['name']."';?\></td>";
expected result in URL is:
.../members.php?id=name&action=ASC
but my is:
.../members.php?id=<?php%20echo%20%27name%27;?\>&action=<?php%20echo%20;?\>
can you anybody help me?
Edit 2
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo '<th>Name</th>';
while($row = $result->fetch_assoc()) {
echo '<td> ' . $row['name'] . '</td>';
Undefined variable: 'row'
can you help me?
Try this. When you echoes that is already between <?php tags.
<?php
echo '<th>Name</th>';
echo '<td> ' . $row['name'] . '</td>';
?>
But I suggest you to use it as a template, not from PHP (this is html code)
<th>Name</th>
<td><?php echo $row['name']; ?></td>;
Try this code 100% working..
echo '<th><a href="members.php?id='$name'&action='$action"'>Name</a></th>';
echo '<td>'$row["name"]'</td>';
I have problem with positioning html tags I did try to solve the problem but for me the code looks fine however the output tells something different I did try do to sort out the output of SQL query however I wasn't able to achieve this and when I did asked for help in the forum in reply I get that it would be much easier to do it in PHP. So I have produce following code but I am not getting required results:
<body>
<div class="container">
<?php
$sdate = '';
foreach($rows as $row) {
if($row['shieldDate'] != $sdate){
$sdate = $row['shieldDate'];
echo '<h2>';
echo $row['shieldDate'],' ','opponent',' ',$row['shieldTeam'];
echo '</h2>';
echo "
<p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>
<table class='table'>
<thead>
<tr>
<th>Player</th>
<th>Score</th>
</tr>
</thead>
<tbody>
";
echo "<tr>";
echo "<td>";
echo $row["firstname"],' ', $row["lastname"];
echo "</td>";
echo "<td>";
echo $row["score"];
echo "</td>";
echo "</tr>";
}else{
echo "<tr>";
echo "<td>";
echo $row["firstname"],' ', $row["lastname"];
echo "</td>";
echo "<td>";
echo $row["score"];
echo "</td>";
echo "</tr>";
}
}
?>
</tbody>
</table>
</div>
</body>
I am trying to acheive following output:
However I am getting:
You don't close your tbody and table tags between two dates.
Each time you encounter a new date, you open a new table tag, but you never close it before having a new h2 date title. Add a line to close it.
$sdate = '';
foreach($rows as $row) {
if($row['shieldDate'] != $sdate){
// It's a new date
if (!empty($sdate)) {
// It's not the first date: close previous table
echo '</tbody></table>';
}
$sdate = $row['shieldDate'];
echo '<h2>';
echo $row['shieldDate'],' ','opponent',' ',$row['shieldTeam'];
echo '</h2>';
echo "<p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>
<table class='table'>
<thead>[...]</thead>
<tbody>";
[...]
} else {
[...]
}
}
if (!empty($sdate)) {
// There has been at least one date (at least one table): close it
echo '</tbody></table>';
}
Note that the [...] content is the same in if and else instructions.
You could do this to have something cleaner. The less duplicate code you have, the more easy it is to read. The more easy to read your code is, the better.
foreach($rows as $row) {
if($row['shieldDate'] != $sdate){
// Do your h2 and open table stuff
}
echo "<tr>";
echo "<td>";
// etc. No need to put it in the if AND in the else instructions.
}
I have a code that display on the page the information of a specific user, from a MySQL table. However, the informaiton is all displayed on a simple line, back to back. What I want, is to display the information of each line in diffrent blocks. So my question: how can I «brake» the line, to create many diffrent blocks of information, that I can put were I want on my page?
<?php
while ($row = mysql_fetch_array($query)) {
echo $row['Column A'] . " " . $row['Column B'] . " " . $row['Column C'] . " ";
} ?>
First of all you should change your mysql_* functions to another functions like PDO & mysqli
About your question is how to generate HTML from your DB data there are multiple of ways
One could be like this
<?php
while ($row = mysql_fetch_array($query)) {
?>
<h1><?php echo $row['columnA']; ?></h1>
<div class="myclass"><?php echo $row['columnb']; ?></div>
<img src="<?php echo $row['columnC']; ?>">
<?php
} ?>
Two could be
<?php
while ($row = mysql_fetch_array($query)) {
$str = "";
$str .= "<h1>".$row['columnA']."</h1>";
$str .= '<div class="myclass">'.$row['columnb'].'</div>';
$str .= '<img src="'.$row['columnC'].'">';
echo $str;
<?php
} ?>
Or you can use a FW wich make it simpler for you
Or you can use a templating library which is the best way for you if you are not going to use a Framework
check out mustache https://github.com/bobthecow/mustache.php
Of course you can change the HTML structure
I hope this can help :)
I think a table might be what you are looking for...
(Don't forget to change to mysqli* as #Sedz mentioned in his answer)
<table border="1">
<?php
while ($row = mysqli_fetch_array($query)) {
echo '<tr>';
echo '<td>' . $row['Column A'] . '</td>';
echo '<td>' . $row['Column B'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td></td>';
echo '<td>' . $row['Column C'] . '</td>';
echo '</tr>';
};
?>
</table>
Arrange the results in rows/cells as you see fit...
Perhaps the best way is you pass the results into an array
$data = array();
while ($row = mysql_fetch_assoc($results)) {
$data[] = $row;
}
And the break you are talking about
foreach ($data as $value) {
echo "$value<br>";
}
It is simple user2619310!
Add "\n" in your line.
Example:
<?php
while ($row = mysql_fetch_array($query)) {
echo $row['Column A'] . " " . $row['Column B'] . " " . $row['Column C'] . "\n";
}
?>
Bye! :-D
How you break the lines can vary depending on what your objectives are and where you're going to use them. Here are some ways of having information on different lines:
Use the br tag at the end of each field.
Use DIVs to contain each row.
Use a table for each row.
DIVs will be the best if you're looking at different browsers and efficient sizing. But tables are also easy to implement for certain things.
Here's how I'd break the rows using tables.
<table>
<?php while ($row = mysql_fetch_array($query)): ?>
<tr>
<td>Column A label</td>
<td><?php echo $row['ColumnA']; ?></td>
</tr>
<tr>
<td>Column B label</td>
<td><?php echo $row['ColumnB']; ?></td>
</tr>
<tr>
<td>Column C label</td>
<td><?php echo $row['ColumnC']; ?></td>
</tr>
<?php endwhile; ?>
</table>
I don't have the slightest clue about your DB table structure but going by what you've shown, I have included an example. This is not tested but it should be good.
Also note the alternative PHP syntax for the while loop.
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'; ?>