ungovernable wrote:Hi,
i have an entry in a table that contains a lot of Names,
separated by line breaks
I would like to display only the lines
which are beginning by the value of $_POST
And, format each lines as a link
for example let's say i have a table in my DB that contains the following:
"Bob Gratton
Petr Kropotkine
Roger Williams"
Then if $_POST is (exemple) Roger
then display only "<a href="Roger.php">Roger</a>"
SCRIPT_find_member_page_070510a
I got interested into howto do this.
So I wrote a complete php application.
This version has been tested at my own Server and works.
It even has got a simple DEBUG, you can turn it ON/OFF
It uses CSV, Comma Separated Values file for storing data (like a little database)
data/members.csv
This is how I organized data:
UserName ---- pagename
Bob Gratton,bob_g.php
Petr Kropotkine,petr_k.php
Roger Williams,roger_w.php
As you can see, Search for a member name MUST have at least 5 characters
to search for. Can of course be changed.
This is a PHP Script with ONLY one echo!!!
echo $display;
at the very end of script ... with whatever messages and such
This a way I would recommend!
The output of my code, is a Small Search-BOX, in upper right corner.
So you can include this in some other of your webpages:
include ( "findmemberpage.php" );
All members pages are put into folder: /pages/
Can be HTML or PHP pages.
I am not sure this is what you were after.
I hope, anyway, somebody may have use for it.
It is a good script to start with
.. and so add and make your own changes and modifications.
The download of THE FULL SCRIPT in a very small ZIP file
is at end of this post.
Enjoy!
halojoy
🙂
Here is my main script
<?php // findmemberpage.php
// Script by halojoy May 2007
// SETTINGS Variables & Paths
$pagetitle= 'Find Member\'s Personal Page';
$datafile = 'data/members.csv';
$pagesdir = 'pages/';
// END SETTINGS
// Optional debug setting
$debug = FALSE; // Set TRUE for script Testing
// HTML Body and Form Background Color & Style
$display = '';
$htmlbeg = '<html><body style="background:lightgrey">' . "\n";
$htmlbeg .= '<div style="float:right;padding:5px;background:lightblue;border:2px solid black">' . "\n";
$htmlbeg .= '<h3 style="text-align:center">' . $pagetitle . '</h3>' . "\n";
$htmlend = '<div>' . "\n";
$htmlend .= '</body></html>';
// Welcome message
$result = '<div style="text-align:center;color:green;font-weight:bold">Welcome!</div>' ."\n";
// Form to enter name
$form =
'<form action="" method="post" style="margin:0">
<span style="color:black;font-weight:bold">Enter Name</span>
<input type="text" name="entry">
<input type="submit" name="make_link" value="Submit">
</form>' . "\n";
// START //////////////////////////////////////////////////
$reqname = '';
$reqlength = 0;
$members = array();
$found = FALSE;
// See if POST Submitted and do Testings of Entry
if ( isset( $_POST[ 'make_link' ]) ) {
$reqname = trim( $_POST[ 'entry' ] );
$reqlength = strlen( $reqname );
$result = ''; // Reset welcome message
if ( $debug === TRUE ) {
$result .= '<b>Entered Name: \'' .$reqname. '\'</b>' . "<br>\n";
$result .= "<br>\n";
}
// Valid entry?
if ( $reqlength < 5 ) { // not valid entry
$result .= '<span style="color:red;font-weight:bold">5 characters or more, please.</span>' ."\n";
$result .= '<span style="font-weight:bold"> Try again!</span>' ."\n";
}
}
if ( $reqlength >= 5 ) { // yes valid entry
// Load members data
if ( ($fp = fopen( $datafile, 'r' )) === FALSE )
exit; // stop if datafile open error
// Read data from file
while ( ( $data = fgetcsv($fp, 96) ) !== FALSE )
$members[] = $data;
fclose( $fp );
// Debug: dump data
if ( $debug === TRUE ) {
$result .= '<b>$members =</b>' . "\n";
$result .= "<pre>\n" . print_r( $members, TRUE ) . "</pre>\n";
}
// Search for requested name in members list
foreach( $members AS $row ) {
if ( strncasecmp( $reqname, $row[0], $reqlength ) == 0 ) {
$fullname = $row[0];
$memberpage = $row[1];
$found = TRUE;
}
if ( $found === TRUE )
break;
}
// Handle Search result
if ( $found === TRUE ) {
$path = $pagesdir.$memberpage;
$link = '<a href="' . $path . '">' . $fullname . ' member page</a>';
if ( $debug === TRUE ) {
$result .= '<h4 style="color:blue;margin-bottom:0px">$fullname = "' .$fullname. '"' . "<br>\n";
$result .= '$memberpage = "' . $path . '"</h4>' . "\n";
}
$result .= '<span style="color:green;font-weight:bold">Page: </span>' ."\n". $link ."\n";
}
else { // Not Found
$result .= '<span style="color:red;font-weight:bold">\'' .$reqname. '\' not found</span>' ."\n";
}
}
// Put HTML Page Source together
$display = $htmlbeg . $form . $result . $htmlend;
// ... and display it
echo $display;
// END OF SCRIPT
exit;
?>
Download the above full script from
my server public downloads:
http://okay.mine.nu/downloads/
look for: SCRIPT_find_member_page_070510a.zip ( 2.8 kB )