Ben P. Dorsi-Todaro
Ben P. Dorsi-Todaro

Reputation: 321

Writing to CSV Issues

The output file doesn't seem to be placing data properly into the CSV

include_once ('database_connection.php');//Including our DB Connection file
if(isset($_GET['keyword'])){//IF the url contains the parameter "keyword"
 $keyword =     trim($_GET['keyword']) ;//Remove any extra  space
$keyword = mysqli_real_escape_string($dbc, $keyword);//Some validation

$query = "select topictitle,topicdescription from topics where topictitle like '%$keyword%' or topicdescription like '%$keyword%'";
//The SQL Query that will search for the word typed by the user .

$result = mysqli_query($dbc,$query);//Run the Query
if($result){//If query successfull
 if(mysqli_affected_rows($dbc)!=0){//and if atleast one record is found
 while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){ //Display the record
 echo '<p> <b>'.$row['topictitle'].'</b> '.$row['topicdescription'].'</p>
 <input type="text" value="'.$_GET['keyword'].'" /><input type="text" value="'.$row['topicdescription'].'" />
 '   ;

                $numbre = fopen($_GET['keyword'].'.csv',"w");
                echo fwrite($numbre, "topictitle,topicdescription\r\n");
                echo fwrite($numbre, $_GET['topictitle'].",&".$_GET['topicdescription']);
                fclose($numbre);

 }
 }else {
 echo 'No Results for :"'.$_GET['keyword'].'"';//No Match found in the Database
 }

}
}else {
 echo 'Parameter Missing in the URL';//If URL is invalid
}

The output file places everything except for the topicdescription.

Upvotes: 0

Views: 51

Answers (2)

MrRaymondLee
MrRaymondLee

Reputation: 536

In the line echo fwrite($numbre, $_GET['topictitle'].",&".$_GET['topicdescription']); do you mean to write $row["topictitle"] and $row["topicdescription"] from the DB query to the CSV rather than URL parameters?

Upvotes: 0

woofmeow
woofmeow

Reputation: 2408

Instead of this line :

echo fwrite($numbre, "topictitle,topicdescription\r\n");

Use this

echo fwrite($numbre, $row['topictitle'] . "," . $row['topicdescription'] . "\r\n");

Also change $_GET['topictitle'] and $_GET['topicdescription'] to $row['topictitle'] and $row['topicdescription']

Upvotes: 1

Related Questions