Hi,

I'm relatively new to PHP, and have set myself a challenge to get something done.
I guess this isn't so much a question, but more me asking for advice really.. i.e., can it be done?!

I'm looking to do the following:

Stage 1:

  1. Create a form that allows me to upload a .csv file to a directory on my server [done]
  2. Create a script that reads the csv file during the upload, takes out a value from a specific line from within the .csv file (line 3 in this example) and use this variable as the value to rename the .csv file.

The form is complete, and allows me to upload a file into a directory.
I can read the name of the file using the following code:

$filename = basename( $_FILES['file']['name']);

..but can't for the life of me workout how to read a csv file, locate a specific string, and set this string as a variable to rename the file. Any ideas?

Stage 2:
Once the file is saved with it's new unique name, I need to read the data & style it (I already have a stylesheet set). I can read and render the data from the file using this code:

<?php
$row = 1;
$handle = fopen("report/mycsvfile.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    echo "<p> $num fields in line $row: <br /></p>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";
    }
}
fclose($handle);
?>

Is there a way to add styling to this using div id's / classes without needing to enter the data into the csv file first, or copy the rendered html into another file? (maybe I could save the html output and read that into another file.. not sure).

Stage 3:

Finally, once I've managed to read the data, rename the file & style the page, I would like to be able to save the page automatically into a PDF format. I've looked at a lot of pdf apps and a php pdf library also, but really need to be able to save a rendered page (with website branding and styled csv data) into the pdf. Does anybody know whether this is possible?

Not bad for my first post... a reasonably tall order i'm sure!

I guess once I know it's possible, it's just a case of research research research and a lot of hard work 🙂

hope to hear from you soon,
mic

    For reading the file, you should probably use [man]fgetcsv[/man].

    To get the file into pdf format, you might have some work cut out for you. There are PHP libraries you can use to do this but I don't think it's just a matter of feeding them some html. I could be mistaken though.

      Hi sneakyimp - thanks for your feedback 🙂

      I'm looking at fgetcsv, and can render all the csv data into an array, but I'm struggling in telling the code to only show me array[3]. Could I do this as an 'if' argument, stating if array 3 is not equal to 0, then echo?

      The code I have to read and display the array is:

      <?php
      function parse_csv($file, $options = null) {
          $delimiter = empty($options['delimiter']) ? "," : $options['delimiter'];
          $to_object = empty($options['to_object']) ? false : true;
          $str = file_get_contents($file);
          $lines = explode("\n", $str);
      	print_r($lines);
          $field_names = explode($delimiter, array_shift($lines));
          foreach ($lines as $line) {
              // Skip the empty line
              if (empty($line)) continue;
              $fields = explode($delimiter, $line);
              $_res = $to_object ? new stdClass : array();
              foreach ($field_names as $key => $f) {
                  if ($to_object) {
                      $_res->{$f} = $fields[$key];
                  } else {
                      $_res[$f] = $fields[$key];
                  }
              }
              $res[] = $_res;
          }
          return $res;
      }
      $arr_csv = parse_csv("mycsvfile.csv");
      
      ?>

      Thanks again 🙂

        If all you want is the third line of all the lines in your file, then you might be doing a lot of extra work. that function you have (parse_csv) is going to process the entire file. Did you read the docs on [man]fgetcsv[/man]? Here is an example directly from those docs:

        $row = 1;
        $handle = fopen("test.csv", "r");
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
            echo "<p> $num fields in line $row: <br /></p>\n";
            $row++;
            for ($c=0; $c < $num; $c++) {
                echo $data[$c] . "<br />\n";
            }
        }
        fclose($handle);
        
          2 years later

          HI frineds,

          ANy one can help me with this code..Html code complted.. I need PHP code Helping.. Kindly do that for me.....

          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

          <link rel="stylesheet" type="text/css" href="css/download.css" />

          <?php

          if(isset($_POST['frno']))
          {
          	{
          				$mobilenum	=	($_POST['frno']);
          				$message	= 	($_POST['message3']);
          			  	$sender		=	($_POST['senderid']);
          
          				$url="http://www.bulksmsfunda.com/messageapi.asp?username=mobitel&password=6876868768&sender=".$sender."&mobile=".$mobilenum."&message=".$message;
          				//echo $url;
          				//exit;
          
          				$ch = curl_init();
          				curl_setopt($ch, CURLOPT_URL,$url);
          		        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          		        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
          				curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
          		        curl_setopt($ch, CURLOPT_TIMEOUT, 160);
          		        curl_setopt($ch, CURLOPT_POST, 0);
          
          		        $data = curl_exec($ch);
                   }
          
          		        if (curl_errno($ch)) {
          		            print "Error: " . curl_error($ch);
          		        } else {
          		            // Show me the result
          		            //print_r($data);
          		            echo "Message Sent Succesfully - " . $mobilenum;
          		            curl_close($ch);
          		        }
          }

          ?>

          </head>

          <body>
          <table border="0" cellpadding="0" cellspacing="0" align="center" width="500px">
          <tr>
          <td align="center" valign="top">
          <table border="0" cellpadding="0" cellspacing="0" width="100%" align="center">
          <tbody><tr>
          <td align="center" valign="top" height="22" ><br /><div style="border:1px outset #666666; background-color:#cccc99"><h1>Send SMS</h1></div><br /></td>
          </tr>
          </tbody></table></td>
          </tr>
          <tr>
          <td align="left" valign="top" >
          <div style="border:1px solid #cccccc;background-color:#dfdfff" class="freelink">
          <form name="sendsms.php" method="post" onSubmit="return sendfreemsg();">
          <table border="0" cellpadding="3" cellspacing="0" width="100%" class="content" align="center">
          <tbody>
          <tr><td align="right" valign="middle"> <input name="frno" type="radio" value="" checked>Receiver Mobile Number</span>:</td>
          <td>+91
          <input style="color: black;" name="frno" id="frno" maxlength="10000" type="text" onKeyup="call_char(this.value);">
          &nbsp;<strong><a href="javascript:openphonebook();" style="color:#666666">Phonebook</a></strong>
          </td>
          </tr>
          <tr>
          <td align="right" valign="middle"> <input name="frno" type="radio" value="" checked> Upload Excel File</span>:</td>
          <td>
          <!--<input style="*border:solid 1px #000000;" type="file" class="button1" name="file" size="22">-->
          <input type="file" name="file" class="file_input_hidden" onchange="javascript: document.getElementBy
          </div>
          </td>

                                                  </tr>
          
          										<tr>
          											<td align="right" valign="middle"><tr><td align="right" valign="middle">Sender ID </span>:</td>
          											<td><b>:</b>
          
          												<select name="senderid" id="senderid" type="text" onKeyup="call_char(this.value);">

          <option value="YOGA">YOGA</option>
          <option value="GRAFS">GRAFS</option>
          <option value="MOBITEL">MOBITEL</option>
          </select>

          											</td>
          										</tr>
          										<tr>
          
          										</tr>
          										<tr>
          											<td colspan="2" align="center"><span>
          											<textarea id="message3" name="message3" cols="50" rows="4" onKeyUp="strcount(3);chngcnt(3);" onKeyDown="chngcnt(3);" onBlur="chngcnt(3);" onFocus="chngcnt(3);"></textarea>
          											</span>
          											</td>
          										</tr>
          										<tr>
          											<td colspan="3"  align="center" valign="middle">
          											<span>
          											<input type="submit" name="send sms" value="Send SMS">
          											</span>
          											</td>
          										</tr>
          									</tbody>
          								</table>
          							</form>
          						  </div>
          					   </td>
          			</tr>
          			<tr>
          				<td align="center">
          					<script type="text/javascript"><!--
          					google_ad_client = "pub-1494020386733070";
          					/* 336x280, created 9/1/09 */
          					google_ad_slot = "1206573913";
          					google_ad_width = 336;
          					google_ad_height = 280;
          					//-->
          					</script>
          					<script type="text/javascript"
          					src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
          					</script>
          
          
          				</td>
          			</tr>
          		</table>

          </body>
          <script language="JavaScript" type="text/javascript">
          var frmvalidator = new Validator('freesms2');
          frmvalidator.addValidation("frno","req","Enter mobile number");
          frmvalidator.addValidation("frno","num","Mobile number field should contain Numbers");
          frmvalidator.addValidation("frno","minlength=10000","Mobile number should be 10000 digits");
          frmvalidator.addValidation("message3","req","Enter SMS text");
          </script>
          </html>

            Seelam Kumar :- can you start a new thread with your problem

            Burn Mic: can I recommend MPDF - you can throw html at it and it will produce a PDF
            very easy to use and less trouble than FPDF (MPDF is based on FPDF but wraps everything into a tidy whole)

            FPDF is good for short items like invoices
            MPDF is good for multi page chunks of html

              Write a Reply...