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']);
Related
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>
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>
This is very quick.
I have a form that when submitted should add user input into my DB. I have had a similar form working well but for some reason this one is not working.
Please excuse my sloppy code but here is the whole page. For some reason 'observation' will not take the user input and display it on the next page (showOne.php). The next page simply shows a blank space for observation and I checked the DB and there was nothing in the observation field of course.
Is it something simple that I can't see? I've been at this for days since it is identical to another form I have that works well on another page.
Thank you for any insight.
<?php
//including the database connection file
include("config.php");
$activePage = "start.php";
if(isset($_POST['update'])) {
$id = $_POST['id'];
$observation = $_POST['observation'];
$result = mysqli_query($mysqli, "UPDATE tasks SET observation='$observation' WHERE id=$id");
header("Location: showOne.php?id=$id");
}
//getting id from url
$id = $_GET['id'];
//selecting data associated with this particular id
$result2 = mysqli_query($mysqli, "SELECT * FROM users WHERE id=$id ORDER BY id ASC");
$result = mysqli_query($mysqli, "SELECT * FROM tasks ORDER BY taskNumber ASC");
include_once "nav.php";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pathfinder - A User Testing Dashboard</title>
<meta name="description" content="Pathfinder - A User Testing Dashboard">
<meta name="author" content="James Stables - Thurs April 13th 2017">
<link rel="stylesheet" href="core.css">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,700" rel="stylesheet">
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"> </script>
<![endif]-->
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<body>
<header>
<h1 class="logo"><img src="images/logo.png">Pathfinder</h1>
<h4>User Testing Dashboard</h4>
</header>
<section class="nonav">
<div class="showAllUsers">
<table border='0'>
<tr>
<?php while($res = mysqli_fetch_array($result2))
{ ?>
<td class="widthHundred no-border left"><h2><?php echo $res['nameFirst']; ?></h2></td>
<?php } ?>
</tr>
</table>
<table border='0'>
<thead>
<tr>
<th>Image</th>
<th>Category</th>
<th>Task Number</th>
<th>Task Name</th>
<th>Observation</th>
<td> </td>
<td> </td>
<td> </td>
</tr>
</thead>
<tbody>
<form name="start" method="post" action="start.php" enctype="multipart/form-data">
<tr>
<td><a href="<?php echo $res['$image'];?>" data-lightbox="example-1" data-title="Task Number <?php echo $res['taskNumber'];?>">
<img id="imageResize" src="<?php echo $res['image'];?>" /></a></td>
<td><?php echo $res['category'];?></td>
<td><?php echo $res['taskNumber'];?></td>
<td><?php echo $res['taskName'];?></td>
<td><input type="text" name="observation" value="<?php echo $res['observation'];?>"></td>
<td> </td>
<td> </td>
<td> </td>
<?php } ?>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td><input type="hidden" name="id" value="<?php echo $_GET['id'];?>"><input type="submit" class="buttonUpdate right" name="update" value="Update"></td>
<td><a class="buttonCancel right" href="tasks.php" onClick="return confirm('Cancel and go back?')">Cancel</a></td>
</tr>
</form>
</tbody>
</table>
</div>
</section>
<script src="js/lightbox-plus-jquery.min.js"></script>
<script>
lightbox.option({
'resizeDuration': 200,
'showImageNumberLabel': false,
'fitImagesInViewport': true
})
</script>
</body>
</html>
I upload some image to phpMyAdmin, and write some code on Dreamweaver.
Then got this:
The code is on below:
<?php
require_once("connection.php");
$result = mysql_query("SELECT * FROM food");
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Food & Beverages</title>
</head>
<body>
<form>
<table width="400" border="1">
<tbody>
<?php
while($row = mysql_fetch_assoc($result)){
?>
<tr>
<td width="240" height="130"><img src="<?php echo $row['image']?>"></td>
<td><?php echo $row['info'];?></td>
</tr>
<?php }?>
</tbody>
</table>
</form>
</body>
</html>
How to solve this problem?
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