the explode() function and the minus sign

Discussion in 'PHP' started by pitto, Dec 8, 2008.

  1. #1
    I wrote a little code that basically turns a csv file into a little table on my website. It looks something like this:

    $row = 1;
    		$handle = fopen(dirname(dirname(__FILE__)) . '/database/file.csv', "r");
    		while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
    		// Break csv strings into title - value format
    			$data_array = explode(',', $data[0]);
    			if ($row%2 != 0) { //for odd tr class=odd_tr
    				echo "\t\t\t<tr class=\"odd_tr\">\n\t\t\t\t";
    			} else { //for even tr class=ie6_oddtr_as_tr
    				echo "\t\t\t<tr class=\"ie6_oddtr_as_tr\">\n\t\t\t\t";
    			}
    			echo "<td>$data_array[0]</td>\n\t\t\t\t"; // output field name
    			echo "<td>$data_array[1]</td>\n\t\t\t"; // output field value
    			echo "</tr>\n"; // close current table row
    			$row++;
    		}
    		fclose($handle);
    PHP:
    The csv file is a simple 2 field file that looks something like
    Heading1, Heading2
    title1, 12.01
    title2, 5.88
    title3, 6.79
    ...

    This worked fine until recently when those numbers like 12.01, 5.88, 6.79 became negative number with a minus sign in front of them:

    Heading1, Heading2
    title1, -12.01
    title2, -5.88
    title3, -6.79
    ...

    Now the code causes the page to render improperly if it renders at all (depending on the browser). In firefox I get a table of one field with
    "Heading1, Heading2, title1," displayed and with others I get a table about a hundred rows long with nothing in it.

    I know that all I have to do is remove the minus sign to make it work or replace the minus sign with it's ascii equivalent, but I want to know whether or not explode() is an appropriate function to use in the case of real numbers. All the examples I see on the internet that use explode() always use strings that only contain words/letters so I feel like I'm not using the right function for my situation. If anyone has some wise words or suggestions I would be very appreciative.

    Thanks,
    pitto
     
    pitto, Dec 8, 2008 IP
  2. octalsystems

    octalsystems Well-Known Member

    Messages:
    352
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    135
    Digital Goods:
    1
    #2
    try this

    <table>
    <?php
    $row = 1;
    $handle = fopen(dirname(dirname(__FILE__)) . '/database/file.csv', "r");
    while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {


    // Break csv strings into title - value format
    $data_array = explode(',', $data[0]);
    if ($row%2 != 0) { //for odd tr class=odd_tr
    echo "\t\t\t<tr class=\"odd_tr\">\n\t\t\t\t";
    } else { //for even tr class=ie6_oddtr_as_tr
    echo "\t\t\t<tr class=\"ie6_oddtr_as_tr\">\n\t\t\t\t";
    }
    echo "<td>$data[0]</td>\n\t\t\t\t"; // output field name
    echo "<td>$data[1]</td>\n\t\t\t"; // output field value
    echo "</tr>\n"; // close current table row
    $row++;
    }
    fclose($handle);
    ?>
    </table>
     
    octalsystems, Dec 8, 2008 IP