I am just running a SQL query to fetch a particular data from the database and would like to display in horizontal way inspite of the vertical format, Can we do something to display the data as required.
For Getting the clear understanding of the question i have attached the Output which i am getting and the output which i required after the SQL query is executed.
Thanks in Advance.
Query:
<?php
include 'connect-db.php';
$query = "SELECT distinct work_component FROM daily_progress_demo WHERE package_no='$package_no'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<center>".$row['work_component']."</center>";
echo "<br />";
}
?>
you can do like this
echo "<table>"
echo "<tr>"
while($row = mysql_fetch_array($result)){
echo "<td>".$row['work_component']."</td>";
}
echo "</tr>";
echo "</table>";
echo $row['work_component']."<br>";
Related
I have a simple project that I am working on and I'm having a hard time finding the code I need to get line breaks in the following table:
<?php
// connect to the database
$host = '###';
$username = '###';
$pass = '####';
mysql_connect($host,$username,$pass) or die(mysql_error());
mysql_select_db("####") or die(mysql_error());
// select everything from the table
$query = "SELECT * FROM Employees";
$result = mysql_query($query) or die(mysql_error());
echo "<table>";
echo "<tr>";
while( ($row = mysql_fetch_array($result)))
{
echo "<td>".$row['employeeid']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['department']."</td>";
}
echo "</tr>";
echo "</table>";
// disconnect from the database
mysql_close();
?>
Everything works correctly and it grabs data from the correct database and table. But when it displays results it is all on the same line ("record1record2record3") and I'd like a line break between employee records.
I've searched this question and it seems like my results all show me an entirely different way of doing this. I've already got the code written and fussed with it a lot to get it working. Can I just make a simple alteration to the above code to get the breaks I want?
The <tr> tags must also be inside the while loop so that they are outputted for each row. They make the rows in the HTML table.
while( ($row = mysql_fetch_array($result)))
{
echo "<tr>";
echo "<td>".$row['employeeid']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['department']."</td>";
echo "</tr>";
}
Attempting to display different data in a table, but am only gettting the most recent result. The only thing I have changed in my code is adding the >CURDATE rather than the most simple *. it has stopped displaying all of the results. can you help?
$result = mysqli_query($con,"SELECT * FROM tripdata WHERE date > CURDATE()");
while($row = mysqli_fetch_assoc($result))
{
echo "<table class='mainpics'>";
echo "<tr><td><a class='mainpic' href='". $row['pageurl'] ."'><img class='mainpic' src='" . $row['mainpic'] . "'/></a></td></tr>";
echo "</table>";
}
mysqli_close($con);
?>
I have a search form on a previous page where I allow users to search for $q. I then query the database for 'keys' LIKE $q. The while loop displays the 'name' and 'weblink' of each matching database entry.
This all works correctly. However, I would like the 'weblink' to display as a clickable link. Ideally it would read as if it were HTML: 'weblink'. I cannot figure out the correct combo of php and html to make both the while loop, and the HTML work.
Any help would be appreciated.
Thanks in advance.
// query database
$query = mysql_query("SELECT * FROM `forumlist` WHERE `keys` LIKE '%$q%'");
// display query results
while($row = mysql_fetch_array($query))
{
echo $row['name'];
echo "<br/>";
echo $row['weblink'];
}
while($row = mysql_fetch_array($query))
{
echo $row['name'];
echo "<br/>";
echo '' . $row['weblink'] . '';
}
Here is my code, part of it is working flawlessly while the other isn't.
<?php
$query = "Select * from Query ORDER BY time DESC";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
?>
<span class="hotspot" onmouseover="tooltip.show('<center><b><?php echo $row['name'] ?></b></center>');" onmouseout="tooltip.hide();">
<?php
echo "<img src='/" . $row['name'] . ".gif'> ";
}
?>
Now there is like 100+ rows, the second $row['name'] is working fine with the loop, but the first one is using the First rwos result for every result.Any Solution?
have you tried assigning $row['name'] to a variable then use the variable to build the HTML?
I'm not sure if this matters but you may want to put a semicolon in your first piece of echo code:
<?php echo $row['name']; ?>
I don't spot any obvious errors-- try adding one line code so it echos the $row['name'] earlier in the loop, which might help you understand better what's going on. See below:
<?php
$query = "Select * from Query ORDER BY time DESC";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
echo $row['name']."<br>"; // <-------- ADD THIS LINE
?>
<span class="hotspot" onmouseover="tooltip.show('<center><b><?php echo $row['name'] ?></b></center>');" onmouseout="tooltip.hide();">
<?php
echo "<img src='/" . $row['name'] . ".gif'> ";
}
?>
I found the problem.
The end </span> was after the loop.
I am trying to loop though my users database to show each username in the table in their own row. I can only get it to show one user but loops through this the number of rows there are. Code is below
<?php
require_once ('../login/connection.php');
include ('functions.php');
$query = "SELECT * FROM users";
$results=mysql_query($query);
$row_count=mysql_num_rows($results);
$row_users = mysql_fetch_array($results);
echo "<table>";
for ($i=0; $i<$row_count; $i++)
{
echo "<table><tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
?>
Thanks
mysql_fetch_array fetches a single row - you typically use it in a while loop to eat all the rows in the result set, e.g.
echo "<table>";
while ($row_users = mysql_fetch_array($results)) {
//output a row here
echo "<tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
You're only fetching one row:
$row_users = mysql_fetch_array($results);
You're never calling a fetch again so you're not really looping through anything.
I'd suggest changing your loop to the following:
echo "<table>";
while ($row = mysql_fetch_array($results)) {
echo "<tr><td>".($row['email'])."</td></tr>";
}
echo "</table>";
The while loop will loop through the results and assign a row to $row, until you run out of rows. Plus no need to deal with getting the count of results at that point. This is the "usual" way to loop through results from a DB in php.
In the new MYSQLI, I use this coding
$query = "SELECT * FROM users";
$results=mysqli_query($con,$query);
$row_count=mysqli_num_rows($results);
echo "<table>";
while ($row = mysqli_fetch_array($results)) {
echo "<tr><td>".($row['id'])."</td></tr>";
}
echo "</table>";
mysqli_query($con,$query);
mysqli_close($con);
Your problem is in this line:
echo "<table><tr><td>".($row_users['email'])."</td></tr>";
You're echoing out a <table> tag again. Remove that so your code looks like this:
echo "<table>";
for ($i=0; $i<$row_count; $i++)
{
echo "<tr><td>".($row_users['email'])."</td></tr>";
}
echo "</table>";
You don't need to output a table within a table the way you're doing it.
$result = mysql_query("SELECT `email` FROM `users`");
$num_emails = mysql_num_rows($result);
echo "<table><caption>There are/is $num_emails email(s)</caption>";
while ($row = mysql_fetch_assoc($result)) {
echo "<tr><td>{$row['email']}</td></tr>";
}
echo '</table>';
Something like that should work.