I want to extract the last eight entries from my database and print them into a two columns table like this:
|1|2|
|3|4|
|5|6|
|7|8|
Is that possible?
This is my code:
$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
$db->connect();
$sql = "SELECT ID, movieno
FROM movies
ORDER BY ID DESC
LIMIT 8 ";
$rows = $db->query($sql);
print '<table width="307" border="0" cellspacing="5" cellpadding="4">';
while ($record = $db->fetch_array($rows)) {
$vidaidi = $record['movieno'];
print <<<END
<tr>
<td>
<a href="http://www.dadadada.com/watch?v=$vidaidi" target="_blank">
<img src="http://img.dadadada.com/vi/$vidaidi/1.jpg" width="123" height="80"></a>
</td>
</tr>
END;
}
print '</table>';
Yes it's possible.
<table border=1><tr>
<?
$count = 0;
$max = 4;
while(your loop){
$count++;
echo '<td>'.$count.' record stuff </td>';
if($count >= $max){
//reset counter
$count = 0;
//end and restart
echo '</tr><tr>';
}
}
?>
</tr></table>
Related
My pagination isn't working correctly. I get the page numbers but each page only has one row when it should have 10 rows. I new to php and am adapting code that I found on this site. When I load the page I can tell by the id number that the result showing is what I would expect the 10th row in the query to be...rows 1-9 are missing.
<?php
//Function to return rows for each page
function getPage($stmt, $pageNum, $rowsPerPage)
{
$offset = ($pageNum - 1) * $rowsPerPage;
$rows = array();
$i = 0;
while(($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC,SQLSRV_SCROLL_ABSOLUTE,$offset + $i)) && $i < $rowsPerPage)
{
array_push($rows, $row);
$i++;
}
return $rows;
}
?>
<?php
// Set the number of rows to be returned on a page.
$rowsPerPage = 10;
$usr = $_SESSION['user'];
if ($_SESSION['admin']="YES") {
$query = "SELECT iID,FirstName,LastName, convert(varchar, SubmitDate, 101) as SubDate,LEFT(ImproveIdea,30) as MyIdea,Status FROM Idea where Status='Pending' ORDER BY iID desc";
}else {
$query = "SELECT iID,FirstName,LastName,convert(varchar, SubmitDate, 101) as SubDate,LEFT(ImproveIdea,30) as MyIdea,Status FROM Idea where SubmitBy='".$usr."' ORDER BY iID desc";
}
$stmt = sqlsrv_query($connect,$query, array(), array( "Scrollable" => 'static' ));
if ( !$stmt )
die( print_r( sqlsrv_errors(), true));
?>
<table class="table table-striped table-bordered">
<thead class="thead-light">
<tr>
<th width="54%" scope="col">Submitted By</th>
<th width="14%" scope="col">Date Submitted</th>
<th width="13%" scope="col">Status</th>
<th width="19%" scope="col">Actions</th>
</tr>
</thead>
<tbody>
<?php
$pageNum = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1;
$page = getPage($stmt, $pageNum, $rowsPerPage);
foreach($page as $row)
$ideanum = $row[0];
$fname = $row[1];
$lname = $row[2];
$submitdate = $row[3];
$idea = $row[4];
$status = $row[5];
echo '<tr>';
echo '<td>'.$ideanum.' '.$fname.' '.$lname.' - '.$idea.'...</td>';
echo '<td>'.$submitdate.'</td>';
echo '<td>'.$status.'</td>';
echo '<td><button type="button" class="btn btn-success"><i class="la la-eye"></i></button><button type="button" class="btn btn-info"><i class="la la-edit"></i></button></td>';
echo "</tr>";
?>
<?php
// Get the total number of rows returned by the query.
// Display links to "pages" of rows.
$rowsReturned = sqlsrv_num_rows($stmt);
if($rowsReturned === false)
die( print_r( sqlsrv_errors(), true));
elseif($rowsReturned == 0)
{
echo "No rows returned.";
exit();
}
else
{
// Display page links.
$numOfPages = ceil($rowsReturned/$rowsPerPage);
for($i = 1; $i<=$numOfPages; $i++)
{
$pageLink = "?pageNum=$i";
print("<a href=$pageLink>$i</a> ");
}
echo "<br/><br/>";
}
?>
</tbody>
</table>
SOLVED: The issue was that I was missing the brackets in my foreach statement as a redditor pointed out to me.
Here is my code:
<table border="2px solid #FFF" width="100%">
<tr>
<td width="50%"><Center>Username:</Center><br /></td>
<td width="50%"><center>Numebr Of Warnings:</center><br /></td>
</tr>
</table>
<?
$query = mysql_query("SELECT * FROM warnings");
$numrows = mysql_num_rows($query);
if($numrows != "0"){
while($row = mysql_fetch_assoc($query)){
$warned = $row['username'];
$by = $row['by'];
$sql = mysql_query("SELECT * FROM warnings WHERE username='$warned'");
$warns = mysql_num_rows($sql);
?>
<table border="2px solid #FFF" width="100%">
<tr>
<td width="50%"><center><b><?php echo $warned; ?></b></center></td>
<td width="50%"><center><b><?php echo $warns; ?></center></b></td>
</tr>
</table>
<?php
}
}
else
echo "No Currently Warned Users";
?>
<hr />
And here is the result:
How can I make it so that instead of showing the 2 results for the user Mrg..... I just want it to show one result with the number or rows there are.
Help would be appreciated.
You can use DISTINCT in your query to avoid duplicate rows in your results:
SELECT DISTINCT username, `by` FROM warnings
Change the mysql_fetch_assoc with mysql_feth_array
You're getting the user with double record. Then for each user you are getting 2 lines
The best way to check it is to add pseudo-points to your code.
$query = mysql_query("SELECT * FROM warnings");
$arr=array();
while($row = mysql_fetch_assoc($query))
{
$arr[]=$row;
}
echo "<pre>";
print_r($arr);
echo "</pre>";
exit;
Here check if the $arr has double entries. Then change the mysql_fetch_assoc with mysql_fetch_array and try again
Here is the table
(by,id,username,date)
a1,1,u1,date
a2,2,u2,date
a3,3,u2,date
a4,4,u2,date
And here is the php code
<?php
$conn = mysqli_connect("localhost","username","password");
mysqli_select_db($conn,"dbname");
$warns = mysqli_query($conn,"select username, count(username) as warncount from dtest.warnings W group by username");
while($line = mysqli_fetch_array($warns))
{
echo $line['username']." has ".$line['warncount']. " warns <br/> ";
}
?>
My If Statement Inside of my while loop Does not seem to Execute Properly. I think It might have something to do with my logic. To me, it seems like it should work but there must be something Im missing. I need the While loop to run and count each time it does. On the fourth loop, I need the code in the if statement to run but that never seems to happen. Can anyone offer a soloution please?
<?php
$input = $_GET['input'];//Note to self $input in the name of the search feild
$terms = explode(" ", $input);
$query = "SELECT * FROM content WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
// connecting to our mysql database
mysql_connect("localhost", "username", "password");
mysql_select_db("database");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
for($i=0; $i < $numrows; $i++){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
$plink = $row ['plink'];
$views = $row ['views'];
if($i== 4){
echo '<td valign="top" "width="248" height="100%">
<table width="100%" border="0">
<tr>
<td align="center" valign="top"><a href='.$link.'>
<img src='.$plink.'width="200" height="151" vspace="5" />
<br><b><a href='.$link.'>'.$title.'</b></a>
<br><strong><span style="line-height:20px">Total views: '.$views.'</span></strong>
</td>
</tr>
</table>
</td><tr>';
}
else{
echo '<td valign="top" "width="248" height="100%">
<table width="100%" border="0">
<tr>
<td align="center" valign="top"><a href='.$link.'>
<img src='.$plink.'width="200" height="151" vspace="5" />
<br><b><a href='.$link.'>'.$title.'</b></a>
<br><strong><span style="line-height:20px">Total views: '.$views.'</span></strong>
</td>
</tr>
</table>'
;
}
}
}
}
else
echo "No results found for \"<b>$input</b>\"";
// disconnect
mysql_close();
?>
You're looping once for each row, but within your for loop, you have a while that is fetching all the rows. You need to get rid of the for loop.So instead of doing this:
for($i=0; $i < $numrows; $i++){
while ($row = mysql_fetch_assoc($query)){
you should do this:
$i = 0;
while ($row = mysql_fetch_assoc($query)){
$i++;
....
On the first iteration of your for loop $i == 1, so your condition won't execute. However, your while loop will empty the result set so that on the second and subsequent iterations of your for loop there are no rows to fetch. Your if condition isn't encountered again.
I'm trying to get an HTML Table output in to a specific format via PHP after it grabs information from a MySQL Database.
In HTML, I can do this:
<table cellpadding="0" cellspacing="0" class="list">
<tr>
<td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name1'">Name1</td>
<td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name2'">Name2</td>
<td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name3'">Name3</td>
</tr>
<tr>
<td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name4'">Name4</td>
<td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name5'">Name5</td>
<td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name6'">Name6</td>
</table>
This works great and looks nice, I have it DIV'd up and formatting nicely.
However, I need to do this by pulling down the names from MySQL and then creating a new cell when needed, then after 3 cells, create a new row.
So far, I have this:
<?php
//PRESENTERS HERE
/* connect to the db */
$connection = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($db, $connection);
/* show tables */
$result = mysql_query('SHOW TABLES', $connection) or die('cannot show tables');
echo '<h1>Presenters:</h1>';
while ($tableName = mysql_fetch_row($result))
{
$table = 'presenters';
/* Get the presenters*/
$result2 = mysql_query('SELECT presenter FROM ' . $table) or die('cannot show data from ' . $table);
}
if (mysql_num_rows($result2))
{
echo '<table cellpadding="0" cellspacing="0" class="list">';
while ($row1 = mysql_fetch_row($result2))
{
echo '<tr>';
foreach ($row1 as $key => $value)
{
echo '<td style="cursor:hand" onclick=\"window.location.href = \'page.php?presenter=' . $value . '">', $value, '</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
?>
But I'm unable to figure out how to get it to behave like the HTML one above. I also can't get the on click events to work.
The PHP form ends up with all entries on a row each, instead of three to a row.
Ahhh I see your problem for the entries in 3 rows and not one.
while($row1 = mysql_fetch_row($result2)) {
echo '<tr>';
foreach($row1 as $key=>$value) {
echo '<td style="cursor:hand" onclick=\"window.location.href = \'page.php?presenter='.$value.'">',$value,'</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
Because each time you retrieve something from the database (your while loop statement) you are opening and closing rows in the table.
You need to have a counter started outside the while loop which counts how many rows you have and when you get to the number of columns you want, reset the counter and close off the table tags.
Something like this:
$columncount = 0;
while($row1 = mysql_fetch_row($result2)){
if($columncount == 0){
echo "<tr>";
}
foreach($row1 as $key=>$value) {
echo '<td style="cursor:hand" onclick=\"window.location.href = \'page.php?presenter='.$value.'">',$value,'</td>';
}
$coloumncount ++;
//assuming you want 3 columns in your table
if($columncount == 3) {
echo "</tr>";
$coloumncount = 0;
}
}
Won't solve your onclick events but it will format the table.
Good luck!
Is the first quote for the onclick attribute accidentally getting escaped on this line?
echo '<td style="cursor:hand" onclick=\"window.location.href = \'page.php?presenter='.$value.'">',$value,'</td>';
You're also using different CSS classes between your example static HTML, and the generated HTML in your PHP sample (class="list" vs. class="db-table").
I've got a box with information that I read from database(news) , but I've got another database(categories) with the category of that box. If the categories are 7 I want to automatically make 7 box.
<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("tnews2") or die(mysql_error());
mysql_query("set names 'utf8'");
$rowsPerPage = 2;
$query1 = "SELECT id,name FROM categories ORDER BY ID";
$result1 = mysql_query($query1) or die(mysql_error()."[".$query1."]");
$query2 = "SELECT id,name,text,img,cat_id FROM news WHERE cat_id=2 ORDER BY ID DESC LIMIT $rowsPerPage";
$result2 = mysql_query($query2) or die(mysql_error()."[".$query2."]");
?>
while($row = mysql_fetch_array($result1)){ ?>
<?php for($i=0; $i<$row['id']; $i++){ ?>
<div class="focusBussines" >
<?php while ($rowB = mysql_fetch_array($result2)){ ?>
<a class="titleMini" href="categories.php?id=<?= $rowB['id'] ?>&cat_id=<?=$rowB['cat_id']?>">Бизнес</a>
<?php } ?>
<table class="table" width="100%">
<?php while ($rowB2 = mysql_fetch_array($result2)){
if($rowB2['cat_id'] == 2){ ?>
<tr>
<td align="left" width="150" >
<img class="pic" src="<?php echo $rowB2["img"];?>" height="120" width="120">
</td>
<td align="left" colspan="100%" id="title" ><a id="Zaglavie" href="novina.php?id=<?= $rowB2['id'] ?>&cat_id=<?=$rowB2['cat_id']?>" > <?php echo $rowB2['name']; ?></a></td>
</tr>
<tr >
<td colspan="100%"><hr/> </td>
</tr>
<?php } } ?>
</table>
</div>
<?php } } ?>
but it doesn't work.
Your problem originates from those unnecessary opening/closing php statements (emphasize added):
$result2 = mysql_query($query2) or die(mysql_error()."[".$query2."]");
>>>>> ?> <<<<<
while($row = mysql_fetch_array($result1)){ >>>>> ?> <<<<<
>>>>> <?php <<<<< for($i=0; $i<$row['id']; $i++){ >>>>> ?> <<<<<
This is the same as writing
$result2 = mysql_query($query2) or die(mysql_error()."[".$query2."]");
while($row = mysql_fetch_array($result1)) {
for($i=0; $i<$row['id']; $i++){ ?>
and increases the readability and reduces possible errors due to missing open/close tags.