I have used the following code found here on a page I am developing and all is working fine.
I would however, like to be able to pull mulitple columns from my database and have them formatted on a table.
I have tried everything and cannot get this to work. Should I be using HTML tables or something else? The code below just displays all columns as one long unformatted row.
<div id="container">
<div id="list">
<ul>
<?php
include("connect.php");
$query = "SELECT id, listorder, description, owner, perc_complete, start_date, end_date FROM acct_project_details WHERE project_id='1' ORDER BY listorder ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$id = stripslashes($row['id']);
$listorder = stripslashes($row['listorder']);
$text = stripslashes($row['description']);
$owner = stripslashes($row['owner']);
$perc_complete = stripslashes($row['perc_complete']);
$start_date = stripslashes($row['start_date']);
$end_date = stripslashes($row['end_date']);
?>
<li id="arrayorder_<?php echo $id ?>">
<?php echo $text; ?>
<?php echo $owner; ?>
<?php echo $perc_complete; ?>
<?php echo $start_date; ?>
<?php echo $end_date; ?>
<div class="clear"></div>
</li>
<?php } ?>
</ul>
</div>
</div>
Many thanks in advance,
John
Use table tr td and TableDnD plugin :
<!-- DOWNLOAD THESE SCRIPTS -->
<script type='text/javascript' src='http://isocra.com/wp-includes/js/jquery/jquery.js?ver=1.10.2'></script>
<script type='text/javascript' src='http://isocra.com/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1'></script>
<script type='text/javascript' src='https://github.com/isocra/TableDnD/blob/master/stable/jquery.tablednd.js'></script>
<script type="text/javascript">
$(document).ready(function() {
$("table.dnd").tableDnD();
});
</script>
<div id="container">
<div id="list">
<table class="dnd">
<?php
include("connect.php");
$query = "SELECT id, listorder, description, owner, perc_complete, start_date, end_date FROM acct_project_details WHERE project_id='1' ORDER BY listorder ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$id = stripslashes($row['id']);
$listorder = stripslashes($row['listorder']);
$text = stripslashes($row['description']);
$owner = stripslashes($row['owner']);
$perc_complete = stripslashes($row['perc_complete']);
$start_date = stripslashes($row['start_date']);
$end_date = stripslashes($row['end_date']);
?>
<tr id="arrayorder_<?php echo $id ?>">
<td><?php echo $text; ?></td>
<td><?php echo $owner; ?></td>
<td><?php echo $perc_complete; ?></td>
<td><?php echo $start_date; ?></td>
<td><?php echo $end_date; ?></td>
</tr>
<?php } ?>
</table>
</div>
</div>
Related
I've been creating a CMS blog and this is one of the pages that accessible by the admins. I'm trying to implement pagination in it.
As you can see, it shows the latest six posts in the first page with the IDs from 1 to 6, but when I click on the forward button, it shows the IDs 1 and 2 again for the other posts in which it should be 7 and 8. Could someone tell me what's causing this bug?
First page
Second page
<!-- Right Side Area Start -->
<div class="col-lg-10">
<div class="card-body bg-info">
<h2 class="large-georgia-white-bold">Top Posts</h2>
</div>
<table class="table table-striped table-hover">
<thead class="thead-dark small-times-white">
<tr>
<th>No.</th>
<th>Title</th>
<th>Date&Time</th>
<th>Author</th>
<th>Comments</th>
<th>Details</th>
</tr>
</thead>
<?php
$SrNo = 0;
global $ConnectingDB;
// Query When Pagination is Active i.e Dashboard.php?page=1
if (isset($_GET["page"])) {
$Page = $_GET["page"];
if ($Page==0||$Page<0) {
$ShowPostFrom=0;
}else{
$ShowPostFrom=($Page*6)-6;
}
$sql ="SELECT * FROM posts ORDER BY id desc LIMIT $ShowPostFrom,6";
$stmt=$ConnectingDB->query($sql);
}
// The default SQL query
else{
$sql = "SELECT * FROM posts ORDER BY id desc LIMIT 0,6";
$stmt=$ConnectingDB->query($sql);
}
while ($DataRows=$stmt->fetch()) {
$PostId = $DataRows["id"];
$DateTime = $DataRows["datetime"];
$Author = $DataRows["author"];
$Title = $DataRows["title"];
$SrNo++;
?>
<tbody class="small-times-black">
<tr>
<td><?php echo $SrNo; ?></td>
<td><?php echo $Title; ?></td>
<td><?php echo $DateTime; ?></td>
<td><?php echo $Author; ?></td>
<td>
<?php $Total = ApproveCommentsAccordingtoPost($PostId);
if ($Total>0) {
?>
<span class="badge badge-success">
<?php
echo $Total; ?>
</span>
<?php } ?>
<?php $Total = DisApproveCommentsAccordingtoPost($PostId);
if ($Total>0) { ?>
<span class="badge badge-danger">
<?php
echo $Total; ?>
</span>
<?php } ?>
</td>
<td> <a target="_blank" href="FullPost.php?id=<?php echo $PostId; ?>">
<span class="btn btn-info">Preview</span>
</a>
</td>
</tr>
</tbody>
<?php } ?>
</table>
<!-- Right Side Area End -->
<!-- Pagination -->
<nav>
<ul class="pagination pagination-lg">
<!-- Creating Backward Button -->
<?php if( isset($Page) ) {
if ( $Page>1 ) {?>
<li class="page-item">
«
</li>
<?php } }?>
<?php
global $ConnectingDB;
$sql = "SELECT COUNT(*) FROM posts";
$stmt = $ConnectingDB->query($sql);
$RowPagination = $stmt->fetch();
$TotalPosts = array_shift($RowPagination);
//echo $TotalPosts."<br>";
$PostPagination=$TotalPosts/6;
$PostPagination=ceil($PostPagination);
//echo $PostPagination;
for ($i=1; $i <= $PostPagination ; $i++) {
?>
<li class="page-item">
<?php echo $i; ?>
</li>
<?php } ?>
<!-- Creating Forward Button -->
<?php if ( isset($Page) && !empty($Page) ) {
if ($Page+1 <= $PostPagination) {?>
<li class="page-item">
»
</li>
<?php } }?>
</ul>
</nav>
</div>
</div>
</section>
<!-- Main area end -->
Your script always reassigns $SrNo = 0 when the page is loaded and starts over. You should add the page * 6 value to it so it becomes aware of the offset. In fact, you're already using that logic for $ShowPostFrom, so you can simply assign the same value to $SrNo and it should work:
if ($Page==0||$Page<0) {
$ShowPostFrom=0;
}else{
$ShowPostFrom=($Page*6)-6;
}
$SrNo = $ShowPostFrom; // <- this is what you should add
If you don't mind modifying $ShowPostFrom, you can drop $SrNo completely and just use $ShowPostFrom to show the number.
I'm struggling to show different news in my page. I want to show each of the news in 1 div and the limit is 4 news. Example, i have 4 different news and i want of every each of it to display separately in divs. Im new to html and php. Can someone give me ideas what the best loop and queries for this?
to easily understand what i want to do here is the picture.
here is what i im doing. i think im lost.
here is my php code for the news..
<div class="content">
<div class="container">
<div class="row">
<div class="col-xs-12 col-md-9">
<div class="fix leftbar floatleft">
<div class="fix left_sidebar">
<div class="news">
<h2><i class="fa fa-newspaper-o"></i> Latest News</h2>
<hr class="carved">
<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)) {
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>
<hr>
<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>
<hr>
<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>
<hr>
<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>
View More News
</div>
</div>
</div>
</div>
Simply like your first loop... with better format
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<div class="fix single_news">
<?php echo $title; ?>
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<hr class="carved"/>
<?php
}
?>
Your loop is taking all article, so you need only one loop like this.
Inside you just need to format only one article, all others will take the same format.
You are looping the output over and over again, you only need the single loop to get the data from the database.
Note this is untested:
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
echo '<div class="fix single_news">';
echo '<div class="single_image">';
echo '<img src="img/coveredcourt.jpg" alt="court">';
echo '</div>';
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<hr>
<?php
}
?>
</div>
View More News
I've got a table called tickets. The Tickets table has 4 columns: user_id, ticket_id, subject, message. I want to loop the ticket_id and subject of the logged in user. I want to echo it with foreach, but I cant get it to work. This is what I've got:
Query:
$showTheTickets = array();
$user_id = $_SESSION['user_id'];
$getTickets = mysqli_query($mysqli,"SELECT * FROM tickets WHERE user_id = '$user_id'") OR die (mysqli_error($mysqli));
while($row = mysqli_fetch_assoc($getTickets)){
$showTicketID = $row['ticket_id'];
$showTicketSubject = $row['subject'];
}
Code in the page:
<?php foreach ($showTheTickets as $showTheTickets): ?>
<div class="preview">
<?php echo $showTicketID ?>
<?php echo $showTicketSubject ?>
</div>
<?php endforeach; ?>
Anyones willing to help me? Thanks.
Try this:
$showTheTickets = array();
$user_id = $_SESSION['user_id'];
$getTickets = mysqli_query($mysqli,"SELECT * FROM tickets WHERE user_id = $user_id") OR die (mysqli_error($mysqli));
while($row = mysqli_fetch_array($getTickets)){
$row = array( 'ticket_id' => $row['ticket_id'], 'subject' => $row['subject'] );
$showTheTickets[] = $row;
}
And then:
<?php foreach ($showTheTickets as $stt): ?>
<div class="preview">
<?php echo $stt['ticket_id'] ?>
<?php echo $stt['subject'] ?>
</div>
<?php endforeach; ?>
Hope it helps...
You are overwriting the values you get from the query and your $showTheTickets array remains empty.
You probably want something like:
$showTheTickets = array();
while($row = mysqli_fetch_assoc($getTickets)){
$showTheTickets[$row['ticket_id']] = $row['subject'];
}
and:
<?php foreach ($showTheTickets as $id => $subject): ?>
<div class="preview">
<?php echo $id; ?>
<?php echo $subject; ?>
</div>
<?php endforeach; ?>
Why not simply
while($row = mysqli_fetch_assoc($getTickets)){
?><div class="preview"><?
echo $row['ticket_id'];
echo $row['subject'];
?></div><?
}
you have to save $showTicketID and $showTicketSubject in a array (could be the same array or 2 arrays) and then in the view show the arrays, like this:
while($row = mysqli_fetch_assoc($getTickets)){
$array[$row['ticket_id']] = $row['subject'];
}
and the view:
<?php foreach ($showTheTickets as $ticketid => $ticketsubject): ?>
<div class="preview">
<?php echo $ticketid ?>
<?php echo $ticketsubject ?>
</div>
<?php endforeach; ?>
Having a little trouble displaying data from a table. Been looking at my code for the past few hours and can't seem to see the problem. What I am trying to do is when a team is clicked on it will go into the players table and display any player that has that team name on the team page. I keep getting a blank page:
index.php
This is the case that launches the team_view.php
case 'view_team':
$team = $_GET['name'];
$teams = get_players_by_team($team);
include('team_view.php');
break;
team_view.php
<?php include '../../view/header.php'; ?>
<?php include '../../view/sidebar_admin.php'; ?>
<div id="content">
<h1>Team Roster</h1>
<!-- display product -->
<?php include '../../view/team.php'; ?>
<!-- display buttons -->
</div>
<?php include '../../view/footer.php'; ?>
team.php
<?php
$team = $team['name'];
$first = $player['first'];
$last = $player['last'];
$age = $player['age'];
$position = $player['position'];
$team = $player['team'];
?>
<table>
<?php foreach ($players as $player) :
?>
<tr>
<td id="product_image_column" >
<img src="images/<?php echo $player['player_id']; ?>_s.png"
alt=" ">
</td>
<td>
<p>
<a href="?action=view_player&player_id=<?php echo
$player['player_id']; ?>">
<?php echo $player['first']; ?>
<?php echo $player['last']; ?>
</a>
</p>
</td>
</tr>
<?php endforeach; ?>
</table>
product_db.php
<?php
function get_players_by_team($team) {
global $db;
$query = 'SELECT * FROM players
WHERE team = :team';
try {
$statement = $db->prepare($query);
$statement->bindValue(':team', $team);
$statement->execute();
$result = $statement->fetch();
$statement->closeCursor();
return $result;
} catch (PDOException $e) {
display_db_error($e->getMessage());
}
}
You never define $players it looks like what you are expecting to be $players is actually $teams.
$teams = get_players_by_team($team);
Additionally youre using $player at the top of the script instead of inside the loop which makes no sense.
Everything works with this code but i want to have a link from product.php back to category and subcategory.
i have 3 parent categories, page1.php, page2.php and page3.php
also for those i have subcategories page1.php?subcat=1,2,3,4 page2.php?subcat=1,2,3,4
This is php code iam using to product.php
if (isset($_GET['id'])) {
$id= $_GET['id'];
$result2 = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1");
$count = mysql_num_rows($result2);
if ($count > 0) {
while($row = mysql_fetch_array($result2)){
$pid = $row["id"];
$pname = $row["name"];
$pprice = $row["price"];
$pnoncategoryid = $row["noncategoryid"];
$pdesc = $row["desc"];
}
// gjeje emrin e categorise dhe nencategorise
$result3=mysql_query("select * from noncategory WHERE ncid=$pnoncategoryid");
while($row=mysql_fetch_array($result3)){
$noncategoryname = $row["ncname"];
$categoryid = $row["categoryid"];
}
$result4=mysql_query("select * from category WHERE cid=$categoryid");
while($row=mysql_fetch_array($result4)){
$categoryname = $row["cname"];
}
//mbaro
At category and subcategory i want to have link:
<h3><?php echo $pname; ?></h3>
<div class="pbr"></div>
<h4>Category:</h4>
<p class="price"><?php echo $categoryname; ?> / <?php echo $noncategoryname; ?></p>
<div class="pbr"></div>
<h4>Description:</h4>
<p class="desc"><?php echo $pdesc; ?></p>
<div class="pbr"></div>
<h4>Price:</h4>
<p class="price"><?php echo $pprice; ?> <?php echo _valuta_ ?></p>
Sorry for my bad english, hope some1 understand me :))
You want to transform text into a hyperlink. Like this?
<?php echo "<a href='category".$categoryid.".php'>".$categoryname."</a>"; ?> /
<?php echo "<a href='category".$categoryid.".php?subcat=".$pnoncategoryid."'>".$noncategoryname."</a>"; ?>