Jump to content


Photo

input csv file into datatable

jquery php html datatable

  • Please log in to reply
4 replies to this topic

#1 jjf3

jjf3

    Member

  • Members
  • PipPip
  • 20 posts

Posted 01 November 2013 - 09:56 AM

Hello all, new to the forum. I have been struggling with a PHP problem for the past week and have turned to you as a last resort. I have everything set up mostly right, but there are specific features that I need from http://datatables.net/index that I would love to feature on my inventory page. What happens on my page, is that the whole csv contents show. But the rows do not hide as in the examples you can see on the datatables website. The search bar also does not appear. These are standard features nestled within the datatables.js code. So I am guess they disappear because of the way the PHP is set up to view the rows. These features are really important for the future functionality of my site as well! I have the PHP code as follows: 


<?php

set_time_limit(0);
function csv_split($line,$delim=',',$removeQuotes=true) { 
#$line: the csv line to be split 
#$delim: the delimiter to split by 
#$removeQuotes: if this is false, the quotation marks won't be removed from the fields 
   $fields = array(); 
   $fldCount = 0; 
   $inQuotes = false; 
   for ($i = 0; $i < strlen($line); $i++) { 
       if (!isset($fields[$fldCount])) $fields[$fldCount] = ""; 
       $tmp = substr($line,$i,strlen($delim)); 
       if ($tmp === $delim && !$inQuotes) { 
           $fldCount++; 
           $i += strlen($delim)-1; 
       } else if ($fields[$fldCount] == "" && $line[$i] == '"' && !$inQuotes) { 
           if (!$removeQuotes) $fields[$fldCount] .= $line[$i]; 
           $inQuotes = true; 
       } else if ($line[$i] == '"') { 
           if ($line[$i+1] == '"') { 
               $i++; 
               $fields[$fldCount] .= $line[$i]; 
           } else { 
               if (!$removeQuotes) $fields[$fldCount] .= $line[$i]; 
               $inQuotes = false; 
           } 
       } else { 
           $fields[$fldCount] .= $line[$i]; 
       } 
   } 
   return $fields; 
} 
$html_body = '<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="/css/demo_page.css">
   <link rel="stylesheet" type="text/css" href="/css/demo_table.css">

<title>CSV Contents</title>
<style type="text/css">
<!--
.style5 {font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; }
.style9 {font-size: 12px}
.logo h1 {position: absolute; top: 5px; right: 5px; font-size:14px; }
.logo img-with-text {float: left; left 5px; font-size:14px;
.img-with-text {text-align: justify; width: 40%; height: 20%;}
.img-with-text img {display: block; margin: 0 auto;}
.search {margin : 0px;}



-->
</style>

<script class="jsbin" src="http://datatables.net/download/build/jquery.dataTables.nightly.js"></script>
<script type="text/javascript" language="javascript" src="/js/jquery.js"></script>


<div class="img-with-text">
    <img src="/image002.png" alt="sometext" />
  <p>WC" is West Coast warehouse- please add approximately 2-3 weeks for the arrival to East Coast.<br><br>
  </p>

</div>

<div class="logo">
<br>Last Updated on '.date("F j, Y, g:i a",time()+3600).'


</h1>
</div>



</head>


  <body id="dt_example">
    <div id="container">
      <h1> Check Inventory Here</h1>

      <table>
        <thead>
          <tr>
            <th>Item No</th>
            <th>Description</th>
            <th>Price</th>
            <th>Available</th>
            <th>Back Ordered</th>
             <th>On Order</th>
            <th>ETA WH</th>
            <th>WC On Hand</th>
            <th>WC On Ord</th>
            <th>WC Order Date</th>



            

          </tr>
        </thead>
        <tbody>
';
$fp=fopen("csv/inventory4.html",'w');
$write=fputs($fp,$html_body,strlen($html_body));
$i=0;
$content = file("webinvt.txt");
foreach($content as $line)
{
  $l=csv_split($line);
  if(!strstr($l[11],"SET"))
  {
  if($i==10)
  {
    $tmp = '<tr>';
    $write=fputs($fp,$tmp,strlen($tmp));
    $i=0;
  }
  $onhand = (int)$l[15];
  $committed = (int)$l[16];
  $avail = $onhand - $committed;
  $wcdate = substr($l[23],4);
  $eastdate = substr($l[19],4);

  if(strstr($l[1],"DISC"))
  {
    $html_body ='<tr ">
    <td>'.$l[0].'</td>
    <td>'.$l[1].'</td>
    <td>'.$l[12].'</td>
    <td>'.$avail.'</td>
    <td>'.$l[17].'</td>
    <td>'.$l[18].'</td>
    <td>'.$eastdate.'</td>
    <td>'.$l[21].'</td>
    <td>'.$l[22].'</td>
    <td>'.$wcdate.'</td>
      </tr>';
    }
  else
  {
    $html_body ='<tr>
    <td>'.$l[0].'</td>
    <td>'.$l[1].'</td>
    <td>'.$l[12].'</td>
    <td>'.$avail.'</td>
    <td>'.$l[17].'</td>
    <td>'.$l[18].'</td>
    <td>'.$eastdate.'</td>
    <td>'.$l[21].'</td>
    <td>'.$l[22].'</td>
    <td>'.$wcdate.'</td>
      </tr> 
';
  }
  
  $write=fputs($fp,$html_body,strlen($html_body));
  $i++;
  }
}

$html_body='
 

        </tbody>
 <tfoot>
          <tr>
           <th>Item No</th>
            <th>Description</th>
            <th>Price</th>
            <th>Available</th>
            <th>Back Ordered</th>
             <th>On Order</th>
            <th>ETA WH</th>
            <th>WC On Hand</th>
            <th>WC On Ord</th>
            <th>WC Order Date</th>
          </tr>
        </tfoot>
      </table>
</body>
</html>';

$write=fputs($fp,$html_body,strlen($html_body));

fclose($fp);


?>




  

Furthermore, I have to include this piece of javascript inside the HTML variable.:

$(document).ready(function() {  $('#example').dataTable();
} );

Any help will be greatly appreciated! 


Edited by jjf3, 01 November 2013 - 09:58 AM.


#2 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,614 posts
  • LocationCheshire, UK

Posted 01 November 2013 - 10:04 AM

or you could just use fgetcsv


moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#3 jjf3

jjf3

    Member

  • Members
  • PipPip
  • 20 posts

Posted 01 November 2013 - 10:07 AM

or you could just use fgetcsv

 

 

I have tried that before. However, if I use that as a standalone method, the entire csv is shown. I need those specific rows only, There are about ten more unnecessary rows in the csv file.  


Edited by jjf3, 01 November 2013 - 10:07 AM.


#4 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,614 posts
  • LocationCheshire, UK

Posted 01 November 2013 - 10:29 AM

$fp = fopen("webinvt.txt");
while ($line = fgetcsv($fp, 1024)) {
    if (conditions met) {
        # use it
    }
    else {
        # don't use it
    }
}
fclose($fp);

moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#5 jjf3

jjf3

    Member

  • Members
  • PipPip
  • 20 posts

Posted 01 November 2013 - 10:37 AM

$fp = fopen("webinvt.txt");
while ($line = fgetcsv($fp, 1024)) {
    if (conditions met) {
        # use it
    }
    else {
        # don't use it
    }
}
fclose($fp);

 

 

I got this to work: 

 

<?php


$file = fopen("webinvt.txt", "r");
$items = array();


while (!feof($file)) {
   $items[] = fgets($file);
}


fclose($file);


var_dump($items);
?>

but how do i selected which rows to show?


Edited by jjf3, 01 November 2013 - 10:43 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com