This might be a very simple question for most of you but I'm not able to find the solution on my own. I just started with basic php and tried to create a very small gallery script , which requests all the images from the gallery directory and echo's them into a table, the table should have 4 column's and is 876 width, i have the following:
<body>
<table border="0" cellpadding="0" cellspacing="0" id="gallery" width="876">
<tbody><?php
$dir = "./gallery/";
$exten = 'jpg';
if ($handle = #opendir($dir))
{
while (false !== ($file = #readdir($handle))) {
$bestand = $dir ."/". $file ;
$ext = pathinfo($bestand);
if($ext['extension'] == $exten)
{
echo "<tr><td><a href='/gallery/timthumb.php?src=". $file ."&h=300&'><img src='/gallery/timthumb.php?src=". $file ."&h=134&w=190' target='_blank'></a></td></tr>" ;
}
}
#closedir($handle);
}
?>
</tbody></table>
</body>
My css is looking like this:
#gallery { border-collapse: separate; empty-cells: hide;
border: 0px; background-color: #FFF; }
#gallery td { border: 0px; width: 190px;
height: 163px; padding-left: 12px; float: left;
padding-top: 10px; vertical-align: top;
color: #000000; background-image: url(../images/bg.png); background-repeat: no-repeat; }
My problem is that all the thumbnails are being echo'd under each other instead of next to each other, i would like 4 columns of 219 width each next to each other and if there are more pictures a new row etc.
Any help would be much appreciated!
Kind regards
Problem solved (thanks a lot to both of your for your help):
<body>
<table border="1" cellpadding="0" cellspacing="0" id="gallery" width="876">
<?php
$dir = "./gallery/";
$exten = 'jpg';
$i = 0;
$files = array();
if($handle = #opendir($dir))
{
while (false !== ($file = #readdir($handle)))
{
$bestand = $dir ."/". $file ;
$ext = pathinfo($bestand);
if($ext['extension'] == $exten)
{
$files[] = $file;
}
}
#closedir($handle);
}
$total = count($files);
$imgPerRow = 4;
$counter = 0;
$i = 0;
if($total > 0)
{
foreach($files as $file)
{
$i++;
$counter++;
if($counter == 1)
{
echo '<tr>';
}
echo "<td><a href='/gallery/timthumb.php?src=". $file ."&h=300&'><img src='/gallery/timthumb.php?src=". $file ."&h=134&w=190' target='_blank'></a></td>" ;
if($counter == $imgPerRow)
{
$counter = 0;
echo '</tr>';
}
elseif($i == $total)
{
for($j = ($counter - $imgPerRow); $j < 0; $j++)
{
echo '<td></td>';
}
echo '</tr>';
}
}
}
else
{
echo '<tr><td></td></tr>';
}
?>
</table>
</body>
In your code provided your creating new rows for each image so they will appear under each other. what you need to do is below:
<body>
<table border="0" cellpadding="0" cellspacing="0" id="gallery" width="876">
<tbody><tr><?php
$dir = "./gallery/";
$exten = 'jpg';
$i =0;
if ($handle = #opendir($dir))
{
while (false !== ($file = #readdir($handle))) {
$bestand = $dir ."/". $file ;
$ext = pathinfo($bestand);
if($ext['extension'] == $exten)
{
echo "<td><a href='/gallery/timthumb.php?src=". $file ."&h=300&'><img src='/gallery/timthumb.php?src=". $file ."&h=134&w=190' target='_blank'></a></td>" ;
}
$i++;
if($i==4){
echo '</tr><tr>';
$i=0;
}
}
#closedir($handle);
}
?>
</tr>
</tbody></table>
</body>
Try this...
$countTr = 0;
if ($handle = #opendir($dir))
{
while (false !== ($file = #readdir($handle))) {
if($countTr % 4 == 0)
echo "<tr>";
$bestand = $dir ."/". $file ;
$ext = pathinfo($bestand);
if($ext['extension'] == $exten)
{
echo "<td><a href='/gallery/timthumb.php?src=". $file ."&h=300&'><img src='/gallery/timthumb.php?src=". $file ."&h=134&w=190' target='_blank'></a></td>" ;
}
if($countTr % 4 == 3)
echo "</tr>";
$countTr++;
}
if($countTr % 4 != 0)
echo "</tr>";
#closedir($handle);
}
Related
This is the output of my current code:
I was wondering if its possible to have the header on the left and the data from a csv file to be on the right?
For reference, here is my code:
<?php
$row = 1;
if (($handle = fopen("Book1.csv", "r")) !== FALSE) {
echo '<table border="1">';
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<thead><tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td>'.$value.'</td>';
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
?>
This is how my csv file looks like:
This can be done with a table with vertical headings, here is a basic example:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h2>Vertical Headings:</h2>
<table style="width:100%">
<tr>
<th>Name:</th>
<td>Bill Gates</td>
<td>Steve Jobs</td>
</tr>
<tr>
<th>Telephone:</th>
<td>555 77 854</td>
<td>123 77 854</td>
</tr>
<tr>
<th>Telephone:</th>
<td>555 77 855</td>
<td>543 77 855</td>
</tr>
</table>
</body>
</html>
To populate the table with php you can do it like this:
$file = fopen("Book1.csv","r");
$data = array();
while($row = fgetcsv($file)) {
$data[] = $row; //Get all the data
}
if($data){
$n_columns = count($data[0]); //Get number of columns
}
echo '<table border="1">';
for ($col = 0; $col < $n_columns; $col++) {
echo '<tr>';
foreach ($data as $row_k => $row) {
if ($row_k == 0) {
echo '<th>';
} else {
echo '<td>';
}
echo $row[$col] ?? '';
if ($row_k == 0) {
echo '</th>';
} else {
echo '</td>';
}
}
echo '</tr>';
}
echo '</table>';
fclose($file);
Result:
Ok i edit my question i think i understand more now how to build a question.
The website: bit DOT ly/1MHItEH
I want to run a php code when span 3 is between 5 & 9.
The code which outputs this is $players and in the bottom there is a html output code
<td><span style='text-shadow: 0 0 10px #ffca00; color: #ffca00; font-size: 20pt;'>". $players ."</span></td>
This php code outputs a html table with different values for every added server.
For example if i add a new server to servers.php it will create a new span for that server with new values. So basicly it reads and outputs different values for every server.
So how can i just run extra php code if the span 3 is between 5 & 9?
<html>
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
tr td, tr th { text-align: center !important; }
tr td.motd, tr th.motd { text-align: left !important; }
</style>
</head>
<body style="width: 100%; margin: 0 auto; height: 20px; ">
<table class='table table-bordered table-striped'>
</br>
<?php
error_reporting(0);
function openserveriai() {
}
function closeserveriai() {
}
function getnextstring(&$data) {
$temp="";
$counter=0;
while (ord($data[$counter++])!=0) $temp.=$data[$counter-1];
$data=substr($data,strlen($temp)+1);
return $temp;
}
function getnextbytevalue(&$data) {
$temp=ord($data[0]);
$data=substr($data,1);
return $temp;
}
function addServer($ip) {
$map = '';
$players = '';
$maxplayers = '';
$servername = '';
$output = '';
$live_server = '0';
$packet = '0';
$packet = "\xFF\xFF\xFF\xFFTSource Engine Query\x00";
$live_server = fsockopen("udp://".$ip);
if(!$live_server)
{
$output = "Off";
}
else
{
fwrite($live_server, $packet);
socket_set_timeout($live_server,1,0);
$junk = fread($live_server,5);
$status = socket_get_status($live_server);
$do = 1;
$server_info= "";
while($do)
{
$str_1 = fread($live_server,1);
$server_info .= $str_1;
$status = socket_get_status($live_server);
if($status["unread_bytes"] == 0) {$do = 0;}
}
fclose($live_server);
if (strlen($server_info) > 0)
{
$success = 1;
$junk = getnextstring($server_info);
$servername = getnextstring($server_info);
$map = getnextstring($server_info);
$junk = getnextstring($server_info);
$junk = getnextstring($server_info);
$players = getnextbytevalue($server_info);
}
if ($players != '') {
$players = $players;
} else {
$players = "0";
}
if ($maxplayers != '')
{
$maxplayers = $maxplayers;
}
else
{
$maxplayers = "0";
}
if ($output != "Full" and $players != "0" or $maxplayers != "0")
{
$output = $output;
}
else
{
$output = "<font color='#FF0000'>Offline</font>";
}
if ($map != '')
{
$map = $map;
}
else
{
$map = "--";
$maxplayers = "--";
$players = "--";
}
if ($servername != '') {
$servername = $servername;
}
else
{
$servername = "--";
}
}
if($players == $maxplayers && $players != '--')
{
$players = "" . $players . "";
}
else if($players > $maxplayers-3 && $players != '--')
{
$players = "" . $players . "";
}
else
{
$players = "" . $players . "";
}
if ( strlen($map) > 19 )
{
$map = substr($map, 0, 19) . '...';
}
echo "
<tbody>
<tr style='background: #10130d;'>
";
if ($map == '--')
{
echo "<td><span style='background-color: #b85c5c; border-radius: .25em; padding: .2em .6em .3em; font-weight: bold; color: #fff; font-size: 10pt;'>Offline</i></span></td>";
}
else
{
echo "<td><span style='background-color: #5cb85c; border-radius: .25em; padding: .2em .6em .3em; font-weight: bold; color: #fff; font-size: 10pt; '>Online</i></span></td>";
}
echo "
<td><span style='color: #fff; font-size: 10pt;'>". $ip ."</span></td>
<td><span style='text-shadow: 0 0 10px #ffca00; color: #ffca00; font-size: 20pt;'>". $players ."</span></td>
</tr>
</tbody>
";
}
openserveriai();
include ('servers.php');
closeserveriai();
?>
</table>
</body>
</html>
You can do something like this
<?php
$total = 5;
if ($total >= 5 && $total <= 9) {
//Between 5 and 9
} else {
// Not between 5 and 9
}
?>
<table class='table table-bordered table-striped'>
<tbody>
<tr>
<td><span>First</span></td>
<td><span>Second</span></td>
<td><span><?php echo $total; ?></span></td>
</tr>
</tbody>
</table>
Then you can call that php page with
require('yourpage.php');
I have this code which creates buttons from files in a directory. I need to organize them in a table with 4 columns. How can I do this?
Here is my code:
$handle = opendir('mydirectory');
if($handle){
while(($entry = readdir($handle)) !== false){
if($entry != '.' && $entry != '..' && $entry != '.htaccess'){
echo "<button onclick=\"location.href='mydirectory/$entry'\" style=\"background-color: #660000;\"><p style=color:white;>$entry</button><br>";
}
}
closedir($handle);
Taking what #Raptor mentioned in the comments you could do something like this:
<style>
#buttons{
width: 300px;
float:left;
}
</style>
<div id="buttons">
<?php
for($i=0;$i <=10; $i++){
echo "<button>Button $i</button>";
}
?>
</div>
These are the working codes that i have done. the program is about accessing information in an xml file such as student's name,id and marks based on group and team number submitted from a form. there are 2 submit buttons if one button is clicked, it will display with an additional element (pic) and the other one without it. so far so good.
but looking at the php codes, i know that i will be facing problems if there are numerous groups and team numbers. there would be a lot of if-else statements and the codes would be very long. i have tried using foreach but i'm not getting the result that i want. are there anyways to simplify this? i have been searching on the web but it seems that i just don't know how to implement them. quite new to this.
<html><style>
.datagrid table { border-collapse: collapse; text-align: center; width: 100%; }
.datagrid {font: normal 12px/150% Arial, Helvetica, sans-serif;
background: #fff;
overflow: hidden;
border: 4px solid #006699;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px; }
.datagrid table td, .datagrid table th { padding: 7px 20px; }
.datagrid table thead th {background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #006699), color-stop(1, #00557F) );
background:-moz-linear-gradient( center top, #006699 5%, #00557F 100% ); filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#006699', endColorstr='#00557F');
background-color:#006699; color:#FFFFFF; font-size:15px; font-weight:bold;
border-left: 1px solid #0070A8; }
.datagrid table thead th:first-child { border: none; }
.datagrid table tbody td { color: #00496B; border-left: 1px solid #E1EEF4;font-size: 13px;font-weight: normal; }
.datagrid table tbody .alt td { background: #E1EEF4; color: #00496B; }
.datagrid table tbody td:first-child { border-left: none; }
.datagrid table tbody tr:last-child td { border-bottom: none; }
</style>
<center>
<form action="" method="post" >
<select name="group">
<option value="csa">CSA</option>
<option value="csb">CSB</option>
</select>
Enter team number:<input type="text" name="teamnum" value="">
<input type="submit" name="submit1" value="With Photo">
<input type="submit" name="submit2" value="Without Photo">
</form>
<form action="index.php">
<input type="submit" value="main">
</form>
</center>
</html>
<?php
if (isset($_POST['submit1']) != '') {
$file = "student.xml";
$xml = simplexml_load_file($file) or die("Unable to load XML file!");
if (isset($_POST['group']) && isset($_POST['teamnum']) != '') {
$teamnum = $_POST['teamnum'];
$group = $_POST['group'];
if ($group == 'csa' && $teamnum == '1') {
$name = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/name');
$id = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/id');
$total = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/total');
//$photo =$xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/pic');
echo "<div class='datagrid'><table border='1' cellpadding='1'>";
echo "<thead><tr><th>Photo</th><th>Name</th><th>ID</th><th>carry marks</th></tr></thead>";
while ((list(, $node) = each($name)) && (list(, $node1) = each($id)) && (list(, $node2) = each($total))) {
echo "<tbody><tr class='alt'>";
echo "<td>1</td>";
echo "<td>$node</td>";
echo "<td>$node1</td>";
echo "<td>$node2</td>";
echo "</tr>";
}
echo "</tbody></table></div>";
}
else if ($group == 'csa' && $teamnum == '2') {
$name = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/name');
$id = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/id');
$total = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/total');
//$photo =$xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/pic');
echo "<div class='datagrid'><table border='1' cellpadding='1'>";
echo "<thead><tr><th>Photo</th><th>Name</th><th>ID</th><th>carry marks</th></tr></thead>";
while ((list(, $node) = each($name)) && (list(, $node1) = each($id)) && (list(, $node2) = each($total))) {
echo "<tbody><tr class='alt'>";
echo "<td>1</td>";
echo "<td>$node</td>";
echo "<td>$node1</td>";
echo "<td>$node2</td>";
echo "</tr>";
}
echo "</tbody></table></div>";
}
else if (isset($_POST['submit2']) != '') {
$file = "student.xml";
$xml = simplexml_load_file($file) or die("Unable to load XML file!");
if (isset($_POST['group']) && isset($_POST['teamnum']) != '') {
$teamnum = $_POST['teamnum'];
$group = $_POST['group'];
if ($group == 'csa' && $teamnum == '1') {
$name = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/name');
$id = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/id');
$total = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/total');
echo "<div class='datagrid'><table border='1' cellpadding='1'>";
echo "<thead><tr><th>Name</th><th>ID</th><th>carry marks</th></tr></thead>";
while ((list(, $node) = each($name)) && (list(, $node1) = each($id)) && (list(, $node2) = each($total))) {
echo "<tbody><tr class='alt'>";
echo "<td>$node</td>";
echo "<td>$node1</td>";
echo "<td>$node2</td>";
echo "</tr>";
}
echo "</tbody></table></div>";
}
else if ($group == 'csa' && $teamnum == '2') {
$name = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/name');
$id = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/id');
$total = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/total');
echo "<div class='datagrid'><table border='1' cellpadding='1'>";
echo "<thead><tr><th>Name</th><th>ID</th><th>carry marks</th></tr></thead>";
while ((list(, $node) = each($name)) && (list(, $node1) = each($id)) && (list(, $node2)= each($total))) {
echo "<tbody><tr class='alt'>";
echo "<td>$node</td>";
echo "<td>$node1</td>";
echo "<td>$node2</td>";
echo "</tr>";
}
echo "</tbody></table></div>";
}
?>
i have found the way to solve this by using foreach
<?php
if (isset($_POST['submit1']) != '') {
$file = "student.xml";
$xml = simplexml_load_file($file) or die("Unable to load XML file!");
if (isset($_POST['group']) && isset($_POST['teamnum']) != '') {
$teamnum = $_POST['teamnum'];
$group = $_POST['group'];
echo "<div class='datagrid'><table border='1' cellpadding='1'>";
echo "<thead><tr><th>Photo</th><th>Name</th><th>ID</th><th>carry marks</th></tr></thead>";
echo "<tbody>";
foreach ($xml->xpath("//student[contains(teamNum, $teamnum) and contains(group, $group)]") as $student) {
echo "<tr class='alt'>";
echo "<td>".$student->picture."</td>";
echo "<td>".$student->name."</td>";
echo "<td>".$student->id."</td>";
echo "<td>".$student->total."</td>";
echo "</tr>";
}
echo "</tbody></table></div>";
}else echo 'no data';
} else {
echo "enter a team's number";
}
?>
the trick is in the xpath. it selects all students that contains submitted values. after that it will be as $student and i will be able to output the element values from there.
i'm using the code below to show a csv file as html table with php.
My issue is how to show only specific columns of the csv file. For example I'd show the columns number 1,5,9,15.
How can be modified the code to select that specific fields?
Thanks in advance, Mattew
<?php
$row = 1;
if (($handle = fopen("donors.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<td style="border-top: 1px solid rgb(111,180,224); border-left: 1px solid rgb(111,180,224); border-bottom: 1px solid rgb(111,180,224);" align="left" bgcolor="#0066cc" height="36" valign="middle" ><b><font color="#ffffff" size="2"> '.$value.' </font></b></td>';
}else{
echo '<td style=" border-bottom: 1px solid rgb(111,180,224);" sdval="9" sdnum="1040;" align="left" bgcolor="#ffffff" height="25" valign="middle"><font color="#000000" size="2"> '.$value.' </font></td>';
}
}
if ($row == 1) {
echo '</tr>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
echo '</center>';
fclose($handle);
}
?>
// before your while loop
$wantedColumns = array(1,5,9,15);
// ...
for ($c=0; $c < $num; $c++) {
if (!in_array($c,$wantedColumns)) continue;
// ....
turn your CSV string into an array:
$data_as_array = explode(',',$data_as_csv);
echo "This is the value in column #2: ".$data_as_array[1];
This solution doesn't consider yet the number of rows, this post does (see accepted answer):
How to create an array from a CSV file using PHP and the fgetcsv function