glenno3071 Posted June 15, 2017 Share Posted June 15, 2017 //Below is a segment of code that prints in a table format a set of tasks. As simple as it should be,I am having difficultly combining the values in to one record. //As my next step would be to write it to a csv file. 4 values to each record of a file. Then more fun after that. Any insight would be greatly appreciated class Tasklist{ private $tArr = array(); public function addTask($newTask){ $this->tArr[] = $newTask; } public function printTask(){ if(is_array($this->tArr) || is_object($this->tArr)){ echo '<table>'; foreach($this->tArr as $Task => $value){ echo '<tr>'; echo '<td>'.'<td>'.$value->getDescription().'</td>'; echo '<td>'.'<td>'.$value->getCompleted().'</td>'; echo '<td>'.'<td>'.$value->getDateCreated().'</td>'; echo '<td>'.'<td>'.$value->getDateCompleted().'</td>'; //The following attempts at combining the 4 string $value, into 1 $txt string produced various errors even before it echo $txt // $txt="$value->getDescription"."$value->getCompleted"."$value->getDateCreated"." $value->getDateCompleted"; // $txt = implode(',', tArr); // $txt = implode(',',$Task); // $txt=implode(',',$value); // $txt=implode("','",($this->tArr as $Task => $value)); echo $txt;// this is just a test } echo '</tr>'; echo '</table>'; } } } Quote Link to comment Share on other sites More sharing options...
Sepodati Posted June 15, 2017 Share Posted June 15, 2017 (edited) Assuming the table get displayed correctly, $value->getDescription() is the value you want combined. This is different from the $value->getDescription you're trying to use when creating $txt. Do you see the difference? I assume you want all four values put into a comma-delimited $txt string for your CSV file? $txt = $value->getDescription() . ',' . $value->getCompleted() . ',' . $value->getDateCreated() . ',' . $value->getDateCompleted(); This is going to be rewritten each time the foreach() loops, though... have you thought through the whole process? Edited June 15, 2017 by Sepodati 1 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 15, 2017 Share Posted June 15, 2017 Since your goal is to write the data to a CSV file, this whole exercise is pointless. PHP has specialized CSV functions which operate directly on arrays. I also have no idea why you're trying to combine the CSV feature with your HTML output. Those have nothing to do with each other. Write a getter method for the task array or make it class itself traversable. Then, outside of the class, get the array, iterate over it and write each array item directly to the CSV file using the above CSV function. <?php class Tasklist implements IteratorAggregate { private $tasks = []; public function addTask(Task $newTask) { if (is_null($newTask)) { throw new InvalidArgumentException('Task may not be null.'); } $this->tasks[] = $newTask; } public function getIterator() { return new ArrayIterator($this->tasks); } } <?php $tasklist = new Tasklist(); // tasks get added ... // open the CSV file $taskCSV = fopen('/path/to/tasks.csv', 'w'); if (!$taskCSV) { throw new RuntimeException('Failed to open CSV file.'); } // get an exclusive lock on the file to prevent concurrent processes from messing with it if (!flock($taskCSV, LOCK_EX)) { throw new RuntimeException('Failed to lock CSV file.'); } // iterate over tasks and write them to the file foreach ($tasklist as $task) { if (fputcsv($taskCSV, [$task->getDescription(), $task->getCompleted(), $task->getDateCreated(), $task->getDateCompleted()]) === false) { throw new RuntimeException('Failed to write task to CSV file.'); } } flock($taskCSV, LOCK_UN); // release lock fclose($taskCSV); 1 Quote Link to comment Share on other sites More sharing options...
glenno3071 Posted June 15, 2017 Author Share Posted June 15, 2017 //Below is a segment of code that prints in a table format a set of tasks. As simple as it should be,I am having difficultly combining the values in to one record. //As my next step would be to write it to a csv file. 4 values to each record of a file. Then more fun after that. Any insight would be greatly appreciated class Tasklist{ private $tArr = array(); public function addTask($newTask){ $this->tArr[] = $newTask; } public function printTask(){ if(is_array($this->tArr) || is_object($this->tArr)){ echo '<table>'; foreach($this->tArr as $Task => $value){ echo '<tr>'; echo '<td>'.'<td>'.$value->getDescription().'</td>'; echo '<td>'.'<td>'.$value->getCompleted().'</td>'; echo '<td>'.'<td>'.$value->getDateCreated().'</td>'; echo '<td>'.'<td>'.$value->getDateCompleted().'</td>'; //The following attempts at combining the 4 string $value, into 1 $txt string produced various errors even before it echo $txt // $txt="$value->getDescription"."$value->getCompleted"."$value->getDateCreated"." $value->getDateCompleted"; // $txt = implode(',', tArr); // $txt = implode(',',$Task); // $txt=implode(',',$value); // $txt=implode("','",($this->tArr as $Task => $value)); echo $txt;// this is just a test } echo '</tr>'; echo '</table>'; } } } Thank you gents, yes the table works and it is an assignment that is builds on the previous week exercise. So yes its probably not the most efficient way to code however it does allow for greater understanding on my part . Thanks again Quote Link to comment Share on other sites More sharing options...
glenno3071 Posted June 15, 2017 Author Share Posted June 15, 2017 Assuming the table get displayed correctly, $value->getDescription() is the value you want combined. This is different from the $value->getDescription you're trying to use when creating $txt. Do you see the difference? I assume you want all four values put into a comma-delimited $txt string for your CSV file? $txt = $value->getDescription() . ',' . $value->getCompleted() . ',' . $value->getDateCreated() . ',' . $value->getDateCompleted(); This is going to be rewritten each time the foreach() loops, though... have you thought through the whole process? Thank you - this one I understand, and is not above my current level of ability Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 15, 2017 Share Posted June 15, 2017 The code is simply wrong and has nothing to do with your task. So unless your goal is procrastination, this doesn't help you at all. I understand that learning something new is very scary for you. But that's what programming is all about. If your mind shuts down whenever you see a function you don't immediately recognize, you've picked the wrong field. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.