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>
Related
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>
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']);
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 am trying to get a Fusion Table SQL response into a basic HTML table. This is for both search engine fodder and for use with google spreadsheets and their importhtml function.
The foreach to turn the response into a table are turning up some unusual responses, like 1 character at a time? Also the response appears to be formatted as something that can easily be made into an array, but my efforts have been futile. Have been working on this for over two days now, and I bet someone understands the formatting and knows the answer far better than I?
<?php
$result = file_get_contents("https://www.googleapis.com/fusiontables/v1/query?sql=SELECT%20*%20FROM%201QN6e86FybBULPekKvvXd_RF1jw01H7bZAJFjhUg&key=AIzaSyAm9yWCV7JPCTHCJut8whOjARd7pwROFDQ");
?>
<!DOCTYPE html>
<html>
<body>
<div id="page">
<h1>A Table of Clicks</h1>
<table class='data'>
<thead>
<tr>
<th>date</th>
<th>fundraiser</th>
<th>name</th>
<th>link</th>
<th>price</th>
<th>image</th>
<th>ip</th>
<th>merchant</th>
</tr>
</thead>
<tbody>
<?php
list($part1, $part2) = explode(' "rows": [[', $result);
$rows = explode(' ], [', $part2);
echo $rows[0]."<br>";
foreach ($rows as $row) {
{
//$array = array($row);
///var_dump($array);
$boxes = explode(",", $row);
foreach ($boxes as $box) {
echo "
<tr>
<td>".$box[0]."</td>
<td>".$box[1]."</td>
<td>".$box[2]."</td>
<td>".$box[3]."</td>
<td>".$box[4]."</td>
<td>".$box[5]."</td>
<td>".$box[6]."</td>
<td>".$box[7]."</td>
</tr>";
}
}
}
?>
</tbody>
</table>
<hr />
</div>
</body>
Use json_decode to parse the response(it's valid JSON):
<?php
$result = file_get_contents("https://www.googleapis.com/fusiontables/v1/query?sql=SELECT%20*%20FROM%201QN6e86FybBULPekKvvXd_RF1jw01H7bZAJFjhUg&key=AIzaSyAm9yWCV7JPCTHCJut8whOjARd7pwROFDQ");
$result=json_decode($result,true);
?>
<!DOCTYPE html>
<html>
<body>
<div id="page">
<h1>A Table of Clicks</h1>
<table class='data'>
<thead>
<tr>
<th><?php echo implode('</th><th>',$result['columns']);?></th>
</tr>
</thead>
<tbody>
<?php
foreach($result['rows'] as $row){
echo '<tr><td>'.implode('</td><td>',$row).'</td></tr>';
}
?>
</tbody>
</table>
<hr />
</div>
</body>
I am new to PHP and I am trying to make a table using two foreach and I am not getting the output I want.
<html>
<head>
<title>Didier Test</title>
</head>
<body>
<h1>Yesso!</h1>
</body>
<table border="1">
<tr>
<th>Books</th>
<th>Price</th>
</tr>
<tr>
<?php foreach($name as $item): ?>
<td><?=$item?></td>
<?php foreach($price as $item2): ?>
<td><?=$item2?></td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
</body>
</html>
I know I am having an issue with my inner foreach, but I don't know how to correct it.
Please let me know.
Well for one thing you are closing your body tag on line 7, before you even output your table.
<body>
<h1>Yesso!</h1>
</body>
I also don't know why you are doing a nested loop on seemingly non-related data. Other than that we would need to see your query.
Since you start the row with <tr> before the first loop, you need to end the row with </tr> after it:
<tr>
<?php foreach($name as $item): ?>
<td><?=$item?></td>
<?php foreach($price as $item2): ?>
<td><?=$item2?></td>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
<?php
for($i = 0; $i< count($name); $i++) {
$item = $name[$i];
$item2 = $price[$i];
?>
<tr>
<td><?=$item?></td>
<td><?=$item2?></td>
</tr>
<?php
}
?>
Try changing your shorthand php output notation <?= with full <?php echo
<html>
<head>
<title>Didier Test</title>
</head>
<body>
<h1>Yesso!</h1>
</body>
<table border="1">
<tr>
<th>Books</th>
<th>Price</th>
</tr>
<tr>
<?php foreach($name as $item): ?>
<td><?php echo $item?></td>
<?php foreach($price as $item2): ?>
<td><?php echo $item2?></td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
</body>
</html>
You probably dont get any output because your php.ini has set not to allow shorthand php '
Oh, and yes, like mentioned by Barmer and Revent, you have some HTML tag-nesting issues as well. Welcome to the wonderful world of PHP & Good luck :)