How to create table and pagination in index page? - php

How should I create a table for my retrieved data from database, and how can I create pagination in that page?
I have used for list but I need a table format and pagination also needs it. I have only 5 records per page.
<?php
date_default_timezone_set('UTC');
include_once('includes/connection.php');
include_once('includes/article.php');
$article = new Article;
$articles = $article->fetch_all();
//print_r($articles);
?>
<html>
<head>
<title>CMS Tutorial</title>
<link rel="stylesheet" type="text/css" href="assets/style.css"/>
</head>
<body>
<div class="container">
CMS
<ol>
<?php foreach ($articles as $article) { ?>
<li><a href="article.php?id=<?php echo $article['article_id']; ?>">
<?php echo $article['article_title']; ?>
</a>
<small>
posted <?php echo date('l jS Y', $article['article_timestamp']); ?>
</small>
</li>
<?php } ?>
</ol>
<br />
<small>admin</small>
</div>
</body>
</html>

this should work for your project, I recommend you to learn about HTML components it will help you a lot.
A tables needs a Head and Body, this is the basic table format:
<table>
<thead>
<th>TITLE 1</th>
<th>TITLE 2</th>
</thead>
<tbody>
<tr>
<td>VALUE FOR TITLE 1</td>
<td>VALUE FOR TITLE 2</td>
</tr>
</tbody>
</table>
And your code ends like this.
<?php
date_default_timezone_set('UTC');
include_once('includes/connection.php');
include_once('includes/article.php');
$article = new Article;
$articles = $article->fetch_all();
//print_r($articles);
?>
<html>
<head>
<title>CMS Tutorial</title>
<link rel="stylesheet" type="text/css" href="assets/style.css"/>
</head>
<body>
<div class="container">
CMS
<table>
<thead>
<tr>
<th>Link</th>
<th>Posted</th>
</tr>
</thead>
<tbody>
<?php foreach ($articles as $key => $article): ?>
<tr>
<td>
<a href="article.php?id=<?php echo $article['article_id']; ?>">
<?php echo $article['article_title']; ?>
</a>
</td>
<td><?php echo date('l jS Y', $article['article_timestamp']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<br />
<small>admin</small>
</div>
</body>
</html>

Related

PHP HTML foreach loop

How would I be able to make a table from php using html with a foreach loop?
My goal is to have a table with href tags for each of the sites. It's just that I'm a newbie to php
<?php
$baseConf = ['site' => [abc.com],[abc.org]];
?>
<!DOCTYPE html>
<html>
<head>
<title>Default Page</title>
</head>
<body>
<table>
<!--<?php //$site; ?>-->
<?php
foreach($site as $value ) {
printf $value;
}
?>
</table>
</body>
</html>
You can use a simple foreach loop to put <a> tag with href in table.
<table>
<?php
$sites=$baseConf['site'];
foreach($sites as $item) {
?>
<tr>
<td>
<a href="<?= $item ?>">
<?=$item?>
</a>
</td>
</tr>
<?php
}
?>
</table>
It is very simple for loop and you can grab it from SO. Still I am solving it for you.
I assume you have a multidimensional array. Let's simplify it.
$baseConf = ['site' => [example.com],[example.org]];
$sites = $baseConf['site']; // we will loop sites now
Now Use the html like this
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Links</th>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < count($site); $i++){?>
<tr>
<th scope="row"><?php echo $i?></th>
<td><?php echo $site[$i]?></td>
</tr>
<?php}?>
</tbody>
</table>
I have fixed my code a bit and this is what worked out:
$baseConf = array(
'site' => array("abc.com","abc.org"),
);
?>
<!DOCTYPE html>
<html>
<head>
<title>Default Page</title>
</head>
<body>
<!--<?php //$site; ?>-->
<?php
foreach ($baseConf as $value) {
//echo "$value\n";
print_r($value);
}
?>
</body>
</html>

open uploaded json file

Im trying to finish a upload tool which can open a json file and show it in a table. The script i posted below (basis.php) is already working properly.
basis.php
$data = file_get_contents("data.json"); // put the contents of the file into a variable
$characters = json_decode($data); // decode the JSON feed
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/pure-min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<table class="pure-table">
<thead>
<tr>
<th>Tijdstempel</th>
<th>Voornaam</th>
<th >NHL E-mailadres</th>
<th>Geboortejaar</th>
<th>Kwaliteit</th>
<th>Moeilijkheidsniveau</th>
<th>Gegevenscontrole</th>
</tr>
</thead>
<?php foreach ($characters as $character) : ?>
<tbody>
<tr >
<td> <?php echo $character->Tijdstempel; ?> </td>
<td> <?php echo $character->Voornaam; ?> </td>
<td> <?php echo $character->Email; ?> </td>
<td> <?php echo $character->Geboortejaar; ?> </td>
<td> <?php echo $character->Kwaliteit; ?> </td>
<td> <?php echo $character->Moeilijkheidsniveau; ?> </td>
<td> <?php echo $character->Gegevenscontrole; ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
Now here comes the problem, I am trying to change the basis.php and adapt it to the following index.php. This way I want to be able to upload a suitable json file instead.
index.php
<!doctype html>
<html lang="nl">
<head>
<meta charset=utf-8>
<title>Basisformulier voor het uploaden van een bestand</title>
</head>
<body>
<h1>Excel bestanden importeren en exporteren</h1>
<form action="basis.php" method="post" enctype="multipart/form-data">
<label for="frm_importfile">Selecteer het bestand:</label>
<input type="file" name="importfile" id="frm_importfile">
<button type="submit">Upload het bestand</button>
</form>
</body>
So I tryed to change the basis.php in this way:
basis.php(not working)
<?php
if(!isset($_FILES['importfile'])){
echo "FOUT: Je hebt geen bestand geselecteerd om te uploaden";
exit;
}
$data = fopen($_FILES['importfile']['tmp_name'], "r");
$characters = json_decode($data); // decode the JSON feed
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/pure-min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<table class="pure-table">
<thead>
<tr>
<th>Tijdstempel</th>
<th>Voornaam</th>
<th >NHL E-mailadres</th>
<th>Geboortejaar</th>
<th>Kwaliteit</th>
<th>Moeilijkheidsniveau</th>
<th>Gegevenscontrole</th>
</tr>
</thead>
<?php foreach ($characters as $character) : ?>
<tbody>
<tr >
<td> <?php echo $character->Tijdstempel; ?> </td>
<td> <?php echo $character->Voornaam; ?> </td>
<td> <?php echo $character->Email; ?> </td>
<td> <?php echo $character->Geboortejaar; ?> </td>
<td> <?php echo $character->Kwaliteit; ?> </td>
<td> <?php echo $character->Moeilijkheidsniveau; ?> </td>
<td> <?php echo $character->Gegevenscontrole; ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
I have been searching for solutions for a long while on the internet but I still cant get the problem solved. I hope that someone can help me and show me how I can make it possible to upload a json file to the basis.php.
Kind Regards,
Sierra
You open the file but you didn't read any data from it.
A simple method to get the contents of a file is file_get_contents
$data = file_get_contents($_FILES['importfile']['tmp_name']);
Did you try
$data = file_get_contents($_FILES['importfile']['tmp_name']);

DataTable in php

Hi i want to implement DataTable in the table to populate data from my db in my php page, the below code i used, but its not working, not getting the data table, instead i am getting the normal table only -
<head>
<link rel="stylesheet" type="text/css" href="../DataTables-1.10.7/media/css/jquery.dataTables.min.css">
<script src="../DataTables-1.10.7/media/js/jquery.dataTables.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="../DataTables-1.10.7/extensions/Responsive/css/dataTables.responsive.css">
<script src="../DataTables-1.10.7/extensions/Responsive/js/dataTables.responsive.js" type="text/javascript"></script>
</head>
<div id="example_wrapper" class="dataTables_wrapper" role="grid">
<div class="fg-toolbar ui-toolbar ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix">
<?php
$sql="select product_id,product_name,original_price,offer_price,product_rating,image_main,stock_in_hand from niad_products order by product_id desc";
$result=$linkID1->query($sql);
$c=#mysqli_num_rows($result);
if($c>=1){?>
<script>
$(document).ready(function() {
$('#example').DataTable( {
responsive: true
} );
} );
</script>
<div id="printable">
<table id="example" class="display" cellspacing="0" width="100%" style="color:#FFF">
<thead>
<tr>
<th>Image</th>
<th>Product Name</th>
<th>Original Price</th>
<th>Offer Price</th>
<th>Rating</th>
<th>Stock</th>
</tr>
</thead>
<tbody>
<?php while($row=mysqli_fetch_array($result)){?>
<tr>
<td>
<?php echo "<img width='100' height='100' src=../product-images/".$row['image_main'] ." />"; ?></td>
<td>
<?php echo $row['product_name']; ?>
</td>
<td>
<?php echo $row['original_price']; ?>
</td>
<td>
<?php echo $row['offer_price']; ?>
</td>
<td>
<?php echo $row['product_rating']; ?>
</td>
<td>
<?php echo $row['stock_in_hand']; ?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<?php } else {echo "No records";} ?>
</div>
</div>
Can anyone guide me with my mistake. It will be very helpful. Thanks

making a print button

I am trying to make a button that would print my database in a contact list format. Where they would be grouped under the Departments that the contacts are added to on the php forms. Like this: (BELOW IS MY CODE FOR THE CONTACT PAGE)
ADMIN
George George#snakdnasd 0282738292392
8432
IT DEPARTMENT
Tyler tyler#askdnasdksan 7492823829292 8321
I have looked around and have tried the usual commands like window.print and just print() but it just prints the actual php page.
<?php
require_once"connection.php";
$contacts = array();
$all_contacts = "select * from contacts where contact_status = '1'";
$sql_all_contacts = $conn->query($all_contacts);
$total_contacts = $sql_all_contacts->num_rows;
while ($row = mysqli_fetch_assoc($sql_all_contacts)) {
$contacts[] = $row;
}
?>
<!DOCTYPE html>
<html>
<head>
<?php include"includes/head.inc"; ?>
</head>
<body>
<div class="wrapper">
<!-- header section -->
<?php include"includes/header.inc"; ?>
<!-- content section -->
<div class="content">
<div class="floatl"><h1><?php echo $total_contacts ?> Contact(s) Total</h1></div>
<a class="floatr" href="insert_contact.php"><input class="cancel_contact_button" type="button" value="New Contact"></a>
<div class="clear"></div>
<hr class="pageTitle">
<table border ="1" style="width:100%" id="contactsTable" class="display">
<thead>
<tr align="left">
<th>Name:</th>
<th>Email:</th>
<th>Department:</th>
<th>Extension:</th>
<th>Cellphone:</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($contacts as $contact) {?>
<tr>
<td><?php echo $contact["name"];?></td>
<td><?php echo $contact["email"]; ?></td>
<td><?php echo $contact["department"]; ?></td>
<td><?php echo $contact["extension"]; ?></td>
<td><?php echo $contact["cellphone"]; ?></td>
<td><i class="fa fa-pencil"></i> | <i class="fa fa-trash-o"></i></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</body>
</html>

Repeated page design when retrieving entry from MySQL database

I just made a PHP script to retrieve entry from database. When I retrieve entry from a table, the page look like this. The header and form below table is repeated. I need help, thanks.
Here's the whole code I'm using:
<?php
session_start();
include('connection.php');
$username='username';
mysql_query("SELECT * FROM regmember WHERE username='$username'");
$query=("SELECT * FROM product");
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="index.css">
<title>Music Light</title>
<div align="center"><h1>Music Light</h1></div>
<div id="menu">
<ul>
<li><?php echo 'Welcome, '.$_SESSION['username']; ?></li>
<li>Home</li>
<li>Product</li>
<li>Profile</li>
<li>Cart</li>
<li>Testimony</li>
<li>Transaction</li>
<li>Logout</li>
</ul>
</div>
<br>
<br>
<div align="center">
<table border="0">
<tr>
<td>ID</td>
<td></td>
<td><?php echo $row[0];?></td>
</tr>
<tr>
<td>Brand</td>
<td></td>
<td><?php echo $row[1];?></td>
</tr>
<tr>
<td>Instrument Type</td>
<td></td>
<td><?php echo $row[2];?></td>
</tr>
<tr>
<td>Price</td>
<td></td>
<td><?php echo $row[3];?></td>
</tr>
<tr>
<td>Stock</td>
<td></td>
<td><?php echo $row[4];?></td>
</tr>
<tr>
<td>Image</td>
<td></td>
<td><img height="150" width="150" src="productimg/<?php echo $row[6];?>"/></td>
</table>
</div>
<div align="center">
<table>
<form name="deleteentry" action="delete.php" method="get">
<tr>
<td>Delete which entry? (enter product id)</td>
<td><input type="text" name="delete"></td>
<td><input type="button" name="deletebutton" value="Delete"></td>
</tr>
</form>
</table>
</div>
<?php
}
?>
<br>
<br>
<div align="center"><p>Description template</p></div>
<footer>
<p align="center">Copyright © 2013 Music Light</p>
</footer>
missing </head> and <body> and </body>
i would suggest that the <html> <head></head> and <body> to be used outside the loop if u donot actually want to repeat. by loop i mean the while loop
You didn't closed your header with </head>
<head>
<link rel="stylesheet" type="text/css" href="index.css">
<title>Music Light</title>
</head> <= Include this
.
.
.
</html> <= Include this as well at the end

Categories