01094: PHPScript As WikiMarkup Source

Summary: PHPScript As WikiMarkup Source
Created: 2009-04-29 18:52
Status: Closed - resolved
Category: Other
From: Sven
Assigned:
Priority: 3
Version: 2.2
OS: W3K/IIS/PHP5

Description: Hi, I'm trying to create a very simple telephone directory (intranet) using a cvs file and a PHP Script. Below is the script I'm using, the issue I'm having is that the script will run, and correctly query the cvs file, but the data array is not being shown, only the ":" (I know the cvs file is being queried correctly, since the number of ":" correspond correctly the the number of entries in the file. I know that this has to be something very obvious, but I'm new to both PHP and PMWiki, and of course, working on a deadline. On the actual PMWiki page, I'm calling the PHP Script (:phpinc CSVSearch:) The input box shows, allows data entry, and displays the corresponding number of ":" when the search button is pressed. Thanks!!!

<?php // global variables $searchPattern = ''; // contains the searchPattern

function search_csv($file, $searchString) { // check if the search string contains nothing if (empty($searchString)) { return false; }

global $searchPattern; $results = array();

/* Construct the search pattern for the preg_match */

// prepare search pattern $keywords = preg_split('/[\s]+/', trim($searchString));

// remove duplicate entries $keywords = array_values(array_unique($keywords));

// escape preg specific characters for ($k = 0; $k < count($keywords); $k++) { $keywords[$k] = preg_quote($keywords[$k], '/'); }

// build the search pattern. Observe that the "i" modifier // stands for a case-insensitive pattern. $searchPattern = '/(' . implode('|', $keywords) . ')/i';

/* go through the .csv file */

$fp = fopen($file, "r");

// advance the file pointer to the next line while ( ($str = fread($fp, 1)) != "\n" ) { // empty statement }

// iterate through each line and while ($fields = fgetcsv ($fp, 1000, ";")) {

// search in the author field if (preg_match($searchPattern, $fields[1] . ' ' . $fields[2])) { $results[] = $fields; } }

fclose ($fp);

return $results; }

// call the function if form has been submitted if (isset($_POST['submitted']) && $_POST['submitted'] = 'done') { $results = search_csv("test.csv", $_POST['keyword']);

/* write the results */

echo "<div style='font-family:verdana; font-size:9pt; color:#000000;'>\n";

if (!$results || count($results) == 0) { echo "<b>No results found.</b>\n"; } else { echo "<b>Found results:</b><br />\n";

// iterate throught the results array for ($i = 0; $i < count($results); $i++) { echo preg_replace($searchPattern, "<span style='color:red;'>\\1</span>", $results[$i][1]) . ": "; echo preg_replace($searchPattern, "<span style='color:red;'>\\1</span>", $results[$i][2]) . "<br />\n"; } } echo "</div>"; } ?>

<!-- <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> --> <form method="post" action="?n=PmWiki.TelephoneDirectory"> <!-- <form method="post" action="CSVSearch.php"> --> <!-- <form method="post" action="#">; --> <input type="hidden" name="submitted" value="done" /> Keywords: <input type="text" name="keyword" /> <input type="submit" value="Search" name="submit" /> </form>

You may want to use \$1 instead of \\1 in preg_replace() calls under // iterate through the results array. --Petko April 29, 2009, at 08:53 PM

Thanks Petko. Turns out the issue is how the "preg_replace" is being interpreted within PMWiki. Removing the "preg_replace" and just using the echo works perfectly. Do you think it would be worthwhile for me to write-up a "mini" cookbook on how to set up using PHPScriptAsWikiMarkupSource and this script as a simple csv database?

If you think other people may need this same functionality, please do. --Petko July 08, 2009, at 08:11 AM