Display information from MySQL - php

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.

Related

Create clicable hyperlink in table using php with sql (ASC/DESC)

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

How to put php variable to html code

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

PHP result set and HTML positioning

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'],'&nbsp','opponent','&nbsp',$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"],'&nbsp', $row["lastname"];
echo "</td>";
echo "<td>";
echo $row["score"];
echo "</td>";
echo "</tr>";
}else{
echo "<tr>";
echo "<td>";
echo $row["firstname"],'&nbsp', $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'],'&nbsp','opponent','&nbsp',$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.
}

Multiple row & column table data into string

Im trying to make a datalist which will have the options as:
ItemCode - Name
Code and Name are in the same table but are in different columns
Ive got it working for just code:
<?php
while($res=mysql_fetch_row($code))
{
$fullname=$res[0];
echo "<option value=$fullname></option>";
}
?>
I tried doing the following for both:
<?php
while(($res=mysql_fetch_row($code)) && ($res2=mysql_fetch_row($name)))
{
$fullname=$res[0]." - ".$res2[0];
echo "<option value=$fullname></option>";
}
?>
However I had no joy, any help would be greatly appreciated.
echo "<option value=$fullname>$fullname</option>";
//instead of $module?
Try this:
<?php
$query="select ItemCode,Name from tablename";
$result=mysql_query($query);
$cols=2;
echo "<table>";
do{
echo "<tr>";
for($i=1;$i<=$cols;$i++)
{ $row=mysql_fetch_array($result);
?>
<td>
<table>
<tr valign="top">
<td>
<b><?=$row['ItemCode'] ?></b><br />
<?=$row['Name'] ?><br />
</td>
<td width="50"> </td> <!-- Create gap between columns -->
</tr>
</table>
</td>
<?
}
} while($row);
echo "</table>";
?>
You may want to use something like this:
while ($row = mysql_fetch_assoc($result)) {
$value = $row['item_code'] . " - " . $row['name'];
echo '<option value="' . $value . '">' . $value . '</option>';
}
Note: mysql_query, mysql_fetch_row and mysql_fetch_assoc are deprecated, use something else.
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

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

Categories