foreach($detail as $i) {
foreach($fields as $z) {
$stock[$x][$z] = $i[$y];
$y++;
}
$y = 0;
$x++;
}
Now i can call whatever row of the file by using this variable: $stock[$v][word] where $v is the number of the row and the second value is the name of the column. That works pretty well. Now, what i want to do is to include variables inside the cvs and have php execute them and print them. However, it wont work because the variables are not parsed as php but as simple text. Is there any way to do this? i tried by encapsulating the variables in <php> and {php} tags but it didnt work either. Please help.
You cant. It is opening the file, not parsing it. If yu do a fopen on a phgp file it will not parse it either. I would make something like bb code [var_1] and preg_replace. Like:
PHP Code:
<? $var_1 = $_POST['email'];//is some@email.com $var_2 = $_POST['pass'];//is somepass $cell = $stock[$v][word]; //let say that $cell = 'Your email is [var_1] and password is [var_2].'; $cell = preg_replace('/\[var_1\]/', $var_1, $cell); $cell = preg_replace('/\[var_2\]/', $var_2, $cell); echo $cell; //Returns: Your email is some@email.com and password is somepass. ?>
So in case of a cvs file what could i use instead of fgetcsv? i need an alternative that also allows me to do the function the code at the bottom does.
You could include() the file with output buffering on, then dump the buffer into a temporary file that could be used with fgetcsv().
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
If I'm understanding things correctly, you don't have to really "convert" the CSV file, just insert <?php...?> sections where you need it in that file. Then include and process it:
PHP Code:
ob_start();
include 'path/to/file.csv';
$csvText = ob_get_clean();
$fh = fopen('php://temp', 'r+');
fputs($fh, $csvText)
rewind($fh);
while(($row = fgetcsv($fh)) !== false) {
// do stuff with array $row
}
However, from a security standpoint, I'm not crazy about this unless you have complete control of that CSV file and are sure no malicious code could end up in there.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Thanks. That works pretty well. Just one problem. I need to have variables in the cvs that are actually being defined AFTER i call the cvs into the code. Like this:
PHP Code:
function buildcat() { ob_start(); include 'web/yogo.csv'; $csvText = ob_get_clean(); $handle = fopen('php://temp', 'r+'); fputs($handle, $csvText); rewind($handle);
foreach($detail as $i) { foreach($fields as $z) { $stock[$x][$z] = $i[$y]; $y++; } $y = 0; $x++; }
for($v=1; $v < $num; $v++) { $linkedword[$v]="<a href=\"s=glossary&w={$v}\">{$stock[$v][word]}</a>"; <-- here i define the variable }
the variable i need to have within the cvs file is $linkedword, but as you may see, this is defined until many lines below because it cant be defined before doing the while and defining $stock. What should i do here? load the cvs file once, do the while and then load it back maybe? im kinda confused.
Either change the processing logic so the variables get defined first, or else all I can see left to do would be to use eval().
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Hmmm but i cant define the variables without loading the csv file first. Thats my dilemma. I tried by moving the while part to the top of the csv file and convert it into a php, then having all the comma separated values defined as a variable and use explode instead of fgetcsv. But it didnt work because i have basically to load the php/csv file into itself which results into an endless loop. Also, i really dont understand the use of eval. The page even says that its use is highly discouraged.
Okay, this is my idea:
1. Load the csv normally.
2. Do while and define $stock
3. Define $linkedword
4. Write the resulting into a temporary cvs with all the $linkedword variables already parsed.
5. Load the temporary cvs file and have the rest of the code execute as normal (from function buildcat())
What do you think? i know it may be not the best way to do it but i cant think of anything else.
its a dictionary. I store the values in a csv file instead of a database because its more practical that way, or at least thats what i think. The code above produces something like this:
Comestibles - Fruits - Orange: a fruit from an orange tree.
Comestibles - Fruits - Apple: a fruit from an apple tree
Comestibles - Meat - Chicken: meat from chicken.
the code is supposed to sort the words by category and subcategory. It works pretty well. My problem was that in the definition i want to include links to other words in the same dictionary if necessary. Thats why i defined $linkedword that will automatically produce the word and the link to that word in particular.
its a dictionary. I store the values in a csv file instead of a database because its more practical that way, or at least thats what i think. The code above produces something like this:
Comestibles - Fruits - Orange: a fruit from an orange tree.
Comestibles - Fruits - Apple: a fruit from an apple tree
Comestibles - Meat - Chicken: meat from chicken.
the code is supposed to sort the words by category and subcategory. It works pretty well. My problem was that in the definition i want to include links to other words in the same dictionary if necessary. Thats why i defined $linkedword that will automatically produce the word and the link to that word in particular.
This would be very trivial if you were using a properly designed database structure...a csv file is not the best solution for relational data, a relational database is.
This would be very trivial if you were using a properly designed database structure...a csv file is not the best solution for relational data, a relational database is.
But, if you are running your script on Windows then it's a very easy thing to use CSV files as if they were a database and access them with SQL.
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
—Tim Berners-Lee, W3C Director and inventor of the World Wide Web
But, if you are running your script on Windows then it's a very easy thing to use CSV files as if they were a database and access them with SQL.
Easy from a scripting perspective maybe...but not very computationally efficient. There are still many loops through the data for complex queries (such as what would be needed for the above) where with a properly designed and indexed database the read-write to disk (as well as raw memory usage) is greatly reduced.
Easy from a scripting perspective maybe...but not very computationally efficient. There are still many loops through the data for complex queries (such as what would be needed for the above) where with a properly designed and indexed database the read-write to disk (as well as raw memory usage) is greatly reduced.
True enough, but the premise of the question is "Since I'm not going to do this the right way how do I proceed?"
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
—Tim Berners-Lee, W3C Director and inventor of the World Wide Web
Bookmarks