saurabh sharma
saurabh sharma

Reputation: 1

how can i using oops in php show csv file

i want to show the data from the database and csv file. I*m using oops in the php where i show data from both the database and csv but in the csv function there is following error:

syntax error, unexpected '$f' (T_VARIABLE), expecting function (T_FUNCTION) in D:\xampp\htdocs\factory\4.php on line 71

Code:

<?php
interface IDatabase {
    function connect();
	function readCSV();
}


class Db implements IDatabase
{
    private $connection;
    private static $instance;

    private function __construct()
    {
        $host = "localhost";
        $user = "root";
        $pass = "";
        $name = "cart";

        $this->connection = new mysqli($host, $user, $pass, $name);
   
   if(mysqli_connect_error()) {
			trigger_error("Failed to conencto to MySQL: " . mysqli_connect_error(),
				 E_USER_ERROR);
		}

   }

    public  function connect()
    {
        if (self::$instance == null) {			
            self::$instance = new Db();
        }

        return self::$instance;
    }

    public function query($sql)
    {
        $result = $this->connection->query($sql);
        $records = array();
        while ($row = $result->fetch_assoc()) {
            $records[] = $row;
        }
        return $records;
    }
}


$db1 = Db::connect();





$query = $db1->query("SELECT * FROM user_info");
foreach($query as $row=>$val)
{ echo '<tr>';
                echo '<td>'.$val['id'].'</td>';
                echo '<td>'.$val['username'].'</td>';
                echo '<td>'.$val['email'].'</td>';
				echo '<td>'.$val['password'].'</td>';
				echo '</tr>';
}				

class csv implements IDatabase{
public function readCSV($f){fclose($f);}



$f = fopen("http://localhost/csv/cr.csv", "r");
while (($line = fgetcsv($f)) !== false) {
        echo "<tr>";
		$data = count($line); 
        foreach ($line as $cell) {
                echo "<td>" . htmlspecialchars($cell) . "</td>";
        }
        echo "</tr>\n";
}

?>

Upvotes: 0

Views: 604

Answers (1)

Rikard Olsson
Rikard Olsson

Reputation: 869

Like it's saying, there is a syntax error. In a class you cant assign a member variabel by calling a function. Also, you cant begin a while-statement directly in a class, that's a method's job.

Here is something more like what you need

class Csv {
     private $_file;

     public function __construct($path) {
          $this->_file = fopen($path, "r");
     }

     public function closeCsv()
     {
           fclose($this->_file);
     }

     public function readCsv() {
        // I strongly recommend not echoing out here. This should just return the content
        return fgetcsv($this->_file)
     }
}

And you'll then use it like

<?php
     $csv = new Csv("/path/to/file.csv");
     while (($line = $csv->readCsv()) !== false) {
         //Do what you wanna do
     }

     //When done, close it
     $csv->closeCsv()

The code isnt tested, but I'll think you get the point. If not, checkout more about OOP, for example in here http://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762

Upvotes: 1

Related Questions