I edited a PHP code that reads an Excel Spreadsheet (.xlsx form) and displays it very well.
But I want an extra column to be added when PHP page displays the Spreadsheet. (For textareas to update one cell in each row.)
What can I do for it?
Thanks.
<?php
if((!empty($_FILES["file"])) && ($_FILES['file']['error'] == 0)) {
$limitSize = 15000000;
require_once "simplexlsx.class.php";
$getWorksheetName = array();
$xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
$getWorksheetName = $xlsx->getWorksheetName();
echo ' <hr>
<div id="datacontent">
';
'<div id="datacontent">';
for($j=1;$j <= $xlsx->sheetsCount();$j++){
echo '<h3>Tablodaki Worksheet: '.$getWorksheetName[$j-1].'</h1>';
echo '<table id="xlsxTable">';
list($cols,) = $xlsx->dimension($j);
foreach( $xlsx->rows($j+1) as $k => $r) {
if ($k == 0){
$trOpen = '<th';
$trClose = '</th>';
$tbOpen = '<thead>';
$tbClose = '</thead>';
}else{
$trOpen = '<td';
$trClose = '</td>';
$tbOpen = '<tbody>';
$tbClose = '</tbody>';
}
echo $tbOpen;
echo '<tr>';
for( $i = 0; $i < $cols; $i++)
echo $trOpen.'>'.( (isset($r[$i])) ? $r[$i] : ' ' ).$trClose;
echo '</tr>';
echo $tbClose;
}
echo '</table>';
}
}
?>
Related
This is a code i used to prase a data through html dom php and echo a data in table form
all data came in perfect table form. but i want to show only 5 out of all data echo in table.at last i used table to echo a content or data i want only to echo 5 data out of all showing out but i am not getting any idea to code this.. any one can help me to do this?
<?php
include('simple_html_dom.php');
$html = file_get_html('http://www.discoverhongkong.com/us/see-do/events-festivals/events-calendar/index.jsp');
$eventRowData = array();
class eventRecord
{
public $dates;
public $eventDescription;
}
;
foreach ($html->find('#event_table tr') as $eventItem) {
$eventDateArray = array();
$oneEventData = new eventRecord;
foreach ($eventItem->find('.date') as $eventDate) {
$oneDateData = array();
$dom = new domDocument('1.0', 'utf-8');
$dom->loadHTML($eventDate);
$dom->preserveWhiteSpace = true;
$hTwo = $dom->getElementsByTagName('div')->item(0);
foreach ($hTwo->childNodes as $dateElement) {
array_push($oneDateData, $dateElement->nodeValue);
}
//print_r ($oneDateData);
array_push($eventDateArray, $oneDateData);
}
//
$oneEventData->dates = $eventDateArray;
$eventData = null;
foreach ($eventItem->find('.event_name') as $eventName) {
$dom = new domDocument('1.0', 'utf-8');
$dom->loadHTML($eventName);
$dom->preserveWhiteSpace = true;
$baseLink = "http://www.discoverhongkong.com";
$img = $dom->getElementsByTagName('a')->item(0);
$relativeLink = $img->attributes->getNamedItem("href")->value;
$eventLink = $baseLink . $relativeLink;
$eventData = '<strong>' . $img->textContent . '</strong><br />';
$oneEventData->eventDescription = $eventData;
//echo $oneEventData->eventDescription;
}
array_push($eventRowData, $oneEventData);
}
$arr = array_values($eventRowData);
//Print_r ($eventRowData);
//echo "----------";
//echo $arr[0]->eventDescription;
//echo "----------";
echo '</br>';
echo '<h2>Events In HongKong</h2>';
echo '<table class="table-bordered">';
echo '<tr>';
echo '<th class="abc">EVENT NAME</th>';
echo '<th>START DATE</th>';
echo '<th>END DATE</th>';
echo '</tr>';
foreach ($arr as $xyz) {
//print_r ($xyz);
echo '<tr>';
echo '<td>';
echo ($xyz->eventDescription);
echo '</td>';
//$mydates = array_values($xyz->dates);
//print_r ($mydates);
foreach ($xyz->dates as $datevalue) {
//echo '<tr>';
echo '<td>';
foreach ($datevalue as $datevalueitem) {
echo $datevalueitem;
echo '/';
}
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
Which of these loops is the one you want to restrict? Whichever one it is, the structure would be the same. Something like this:
$i = 0;
foreach ($collection as $item) {
$i++;
if ($i > 5) {
break;
}
// the rest of your loop code
}
So you're basically storing in $i (or whatever you want to call the variable) a count of how many times the loop has executed. After 5 times, you use break; to exit the loop, regardless of how many records remain.
I have a 'map' kind of thing for something im working on. Im drawing each tile out and then checking against a database to see if somebody is located at that tile.
The code works but only for the first result in the db.
Can anyone help. many thanks.
$sqlw = "SELECT id, player_coord_x, player_coord_y FROM player_game WHERE world_id='$world'";
$world_result = $player_stat->query($sqlw);
?>
<div class='map-grid'>
<?
$id = '';
$size = 16;
for($i = 1; $i <= $size; $i++) {
echo "<div class='map-grid-row'>";
for ($j=1; $j <= $size; $j++) {
// check for player at location
if ($world_result->num_rows > 0) {
while($w_row = $world_result->fetch_assoc()) {
$player_coord_x = $w_row['player_coord_x'];
$player_coord_y = $w_row['player_coord_y'];
$id = $w_row['id'];
}
}
if ($player_coord_x == $i and $player_coord_y == $j){
echo "<div class='map-grid-cell high'>";
echo "XXX";
echo "</div>";
}else{
echo "<div class='map-grid-cell high'>";
echo "<span class=\"map-small\">(x-$i y-$j)</span>";
echo "</div>";
}
}
echo "</div>";
}
?>
</div>
$sqlw = "SELECT id, player_coord_x, player_coord_y FROM player_game WHERE world_id='$world'";
$world_result = $player_stat->query($sqlw);
if($world_result->num_rows > 0){
while($w_row = $world_result->fetch_assoc()){
//first type of array
$player[] = array(
'x' => $w_row['player_coord_x'], //your player X record
'y' => $w_row['player_coord_y'], //your player Y record
'id' => $w_row['id'] //your player id
);
//second type of array
// OR store player's X and Y as key of array
$player[$w_row['player_coord_x'] . '-' . $w_row['player_coord_y']] = $w_row['id'];
// This are only if the player record will never repeat
}
}
?>
<div class="map-grid">
<?php
$size = 16;
for($i = 1; $i <= $size; $i++){
echo '<div class="map-grid-row">';
for($j = 1; $j <= $size; $j++){
//with first type of array
$exists = false;
foreach($player as $play){
if($play['x'] == $i && $play['y'] == $j){
$exists = true;
break; // break the loop once you got the result.
}
}
if($exists){ //Player exists? if no detect in foreach loop above, default exists would return false.
echo '<div class="map-grid-cell high">';
echo 'XXX';
echo '</div>';
} else {
echo '<div class="map-grid-cell high">';
echo '<span class="map-small">(x-' . $i . ' y-' . $j . ')</span>';
echo '</div>';
}
//second type of array
//Check if the key exists and if it's empty.
if(isset($player[$i.'-'.$j]) && !empty($player[$i.'-'.$j])){
echo '<div class="map-grid-cell high">';
echo 'XXX';
echo '</div>';
} else {
echo '<div class="map-grid-cell high">';
echo '<span class="map-small">(x-' . $i . ' y-' . $j . ')</span>';
echo '</div>';
}
}
}
?>
</div>
A few months ago, I made a system where people could upload a picture and then those pictures would be displayed in a table with 4 columns.
Now I am working on a project (to practice my php, I am still learning it) and I would like to use a similar system as below but it should display data from a database.
The result I was trying to achieve is the data being displayed in 2 columns.
if ($handle = opendir('images/')) {
$bool = true;
while ($bool) {
$td = 1;
echo '<tr>';
while ($td < 4) {
$file = readdir($handle);
if ($file != '.' && $file != '..' && $file != '.DS_Store') {
echo '<td>';
if ($file) {
echo '<img src="images/'.$file.
'">';
} else {
$bool = false;
}
echo '</td>';
$td++;
}
}
echo '</tr>';
}
}
closedir($handle);
For anyone looking for the answer:
$query = "SELECT * FROM webshop";
if($query_run = mysql_query($query)) {
$bool = true;
echo '<table>';
while($bool) {
$td = 0;
echo '<tr>';
while($td < 2) {
if($fetch = mysql_fetch_assoc($query_run)) {
echo '<td>';
$title = $fetch['title'];
$info = $fetch['info'];
/* Commented out for now, will be implemented later
$image = $fetch['image'];
$price = $fetch['price'];
*/
echo $title;
} else {
$bool = false;
}
echo '</td>';
$td++;
}
echo '</tr>';
}
echo '</table>';
}
CODE:
<?php
$page = $_GET['page'];
$category = $_GET['cat'];
$your_db = # new mysqli("database","name","password");
if (mysqli_connect_errno())
{
echo 'ERROR!
'.mysqli_connect_errno()
.' - Not connected : '.mysqli_connect_error().'
';
die;
}
else
{
$db_connect = $your_db->select_db("databasename");
if (!$db_connect)
{
echo 'ERROR CONNECT DATA BASE';
die;
}
}
echo '<p>';
$query = "select distinct fldCity from info order by fldCity";
$result = $your_db->query($query);
$number_of_records = $result->num_rows;
for ($i = 0; $i < $number_of_records; $i++)
{
$row = $result->fetch_assoc();
echo '<a href="index.php?cat='.stripslashes($row['fldCity']).'">';
echo stripslashes($row['fldCity']);
echo '</a> / ';
}
echo '</p>';
if ($category)
{
$query = "select fldCompanyLogo, fldCompanyName, fldAddress, fldCity, fldProvince, fldPostalCode, fldMenuLink
from info where fldCity = '$category' and length(fldCompanyLogo) > 0 order by fldCity";
}
else
{
$query = "select fldCompanyLogo, fldCompanyName, fldAddress, fldCity, fldProvince, fldPostalCode, fldMenuLink
from info where length(fldCompanyLogo) > 0 order by fldCity";
}
$result = $your_db->query($query);
$number_of_records = $result->num_rows;
$num_pages = $number_of_records / 7;
if (($number_of_records % 7) > 0 )
{
$num_pages++;
}
if (strlen($page) == 0)
{
$page = 0;
}
else
{
$page = $page * 4;
}
echo '<table border="0" cellspacing="10" cellpadding="10" style="border-collapse: collapse" bordercolor="#111111">';
$row_num = 4;
$result->data_seek($page);
for ($i = $page; $i < $number_of_records; $i++)
{
if ($row_num <= 4)
{
echo '<tr>';
for ($col_num = 0; $col_num < 4; $col_num++)
{
$row = $result->fetch_assoc();
echo '<td>';
show_image(stripslashes($row['fldCompanyLogo']),stripslashes($row['fldCompanyName']));
echo '
';
echo '<b><font face="Tahoma" size="2"><a href="mysite.ca/'.stripslashes($row['fldMenuLink']).'" target="_blank"></font>';
echo '<font face="Tahoma" size="2"><a href="'.stripslashes($row['fldMenuLink']).'" target="_blank">';
echo stripslashes($row['fldCompanyName']);
echo '</a>';
echo '
';
echo stripslashes($row['fldCity']);
echo '
';
echo stripslashes($row['fldProvince']);
echo '
';
echo stripslashes($row['fldPostalCode']);
echo '</td>';
}
$row_num++;
echo '</tr>';
}
else
{
break;
}
}
echo '</table>';
for ($j = 0; $j < $num_pages; $j++)
{
$page_link = $j + 1;
echo '<font face="Tahoma" size="4"><b>'.$page_link.'</b></font> ';
}
echo ' '.$number_of_records;
function show_image($image_name)
{
if (file_exists("'$image_name'"))
{
$dim_img = getimagesize('/images/'.$image_name);
echo '<img src="/images/'.$image_name.'" alt = '.$alt.' border=0 align="bottom"';
echo 'width = '. $dim_img[100] .' height = ' .$dim_img[100] . ' />';
}
else
echo 'Add your image here!';
}
?>
I am totally lost and I can't find the error to why the images from the database isn't loading with the script. I highlighted the area in blue where I believe the error is to be. I just can't find any errors in that particular spot! All I want is the images from a column I have in my database to be fetched and connected to 'echo '
No images are being displayed, and I can't find the error as to why the images aren't showing up
try Convert Image to Base64 String and Base64 String to Image
also why echo ''; you use this its makes no sense
could you please help me out?
I need to show a registry of a .dbf table in php and have the following code.
<html>
<form method="get">
<input type="text" name="nrdoc"></input>
<input type="submit"></input>
</form>
<?php
if(isset ($_GET['nrdoc'])){
$thefile = "winges/vcabdoc.DBF";
if($thefile){
include('./dbf_class.php');
$dbf = new dbf_class($thefile);
$num_rec=$dbf->dbf_num_rec;
$field_num=$dbf->dbf_num_field;
$endexct = $timer->end();
echo("<blockquote>File Name : $thefile<br>Number of Records : $num_rec<br>Number of Fields : $field_num</blockquote>");
echo('<table border=1 cellspacing=0>');
echo('<tr>');
echo('<td>No. </td>');
for($j=0; $j<$field_num; $j++){
echo '<td> '.$dbf->dbf_names[$j]['name'];
if ($dbf->dbf_names[$j]['type']!='M') {
echo '<br>Length='.$dbf->dbf_names[$j]['len'];
}
echo '<br>Type='.$dbf->dbf_names[$j]['type'].'</td>';
}
echo '</tr>';
$i=$_GET['nrdoc'];
if ($row === $dbf->getRow("SELECT *, FROM winges/vcabdoc.dbf WHERE 'VCANUM' == $i")) {
echo('<tr>');
echo('<td align="right">'.str_pad($i+1, 3, "0", STR_PAD_LEFT).'</td>');
for($j=0; $j<$field_num; $j++){
if ($dbf->dbf_names[$j]['type']=='N') {
echo '<td align="right">';
}
else {
echo '<td align="left">';
}
echo htmlentities($row[$j]).' </td>';
}
echo '<tr>';
}
}
echo('</table>');
}
?>
</html>
The dbf_class.php is from http://www.phpclasses.org
And it always returns the first data line on the database, please help me.
Thanks in advance.
João
I found an easy way to extract data from .dbf using perl library XBASE
Using this library I wrote a small scripts which reads given file and outputs a json string.
# foxpro2json.pl
use File::Basename;
use XBase;
$filename=$ARGV[0];
my $table = new XBase $filename or die XBase->errstr;
my #fields = $table->field_names;
my $cursor = $table->prepare_select();
my $return = '';
my $i = 0;
while (my #row = $cursor->fetch) {
$json = '{';
$i = 0;
foreach $val (#row) {
$val =~ s/(['"\/\\])/\\$1/g;
$json .= '"'.$fields[$i].'":"'.$val.'",';
$i++;
}
$json = substr($json, 0, -1);
$json .= '},';
$return .= $json;
}
$return = substr($return, 0, -1);
print '['.$return.']';
This is how I call this file inside my php code:
exec('/usr/bin/perl /var/www/foxpro2json.pl /var/www/myfilename.dbf', $json);
$array = json_decode($json[0], true);