Hi All,
Here is the problem:
I have a quey building form that returns results from a database as a form to generate
a new query.
On open, it shows a form, then on submit it shows resutls that are in a form
(checkboxes that let
you omit that record from the next query by $id).
What I want to do is let the user save the page to show others in his group. First I
though of
saving the query, but then it seemed simpler to just save the resutling html to a file
that could be
referenced in email.
How do you get the current page to slurp into a file along with all the current
variables?
On an unrelated note, it seems like I'm generating the restults twice. Any input on
how to
stream-line it?
full script:
<?php
if ($save) { //save is a hidden field, will always be true so that I can test fwriting
to saved.html
$fp = fopen("/home/www/html/locations/site/saved.html", w);
//$fp = fopen("saved.html", w);
//fflush($fp);
//fpassthru($fp);
fwrite($fp, ??);
}
# ln -s index.php omit.php
# This file is a search form/query builder with the ability to
# pare down a result set.
function Refine($query, $to_omit) {
$query = str_replace("order by locations.id", "AND ", $query);
/* //debug:
foreach ($HTTP_POST_VARS as $k => $v){
echo "k: $k, v: <pre>$v</pre>";
}
*/
if ($to_omit) {
trim($to_omit);
$arr_to_omit = explode(" ", $to_omit);
array_pop($arr_to_omit);
foreach($arr_to_omit as $v) {
$query .= "locations.id != '$v' AND \n";
}
}
$query = chop($query);
$query = substr_replace($query, '', -3, 3);
$query .= " order by locations.id";
$query = str_replace("~", "'", $query);
return $query;
}
?>
<html>
<head><title>Locations search</title>
<link rel="stylesheet" href="images/klamath.css" type="text/css">
<script language="JavaScript">
<!-- hide from old browser//
function checkOmit() {
var form = document.refine
form.to_omit.value = ''
for (var i = 0; i < (form.omit.length); i++) {
if (form.omit[i].checked) {
form.to_omit.value += (form.omit[i].value) + ' '
}
}
return true
}
//quit hiding -->
</script>
</head>
<body bgcolor="#6E89AB">
<?php
$db = mysql_connect("localhost", "user", "password")
or die ("No connection");
mysql_select_db("locations",$db)
or die ("No select db");
if ($submit == 'refine') {
Refine(&$query, $to_omit);
//debug:
// echo "<h2>After function calll we get:</h2>\"$query\"";
/* //debug:
$headers = getallheaders();
while (list ($header, $value) = each ($headers)) {
echo "$header: $value<br>\n";
}
*/
$res = mysql_query ($query, $db)
or die ("<br>Bad query, comrade. line 77, number: ".mysql_errno().", ".
mysql_error());
$query = str_replace("'", "~", $query); //gotta hack 2wice
$numrows = mysql_numrows($res);
if($numrows < 1) {
echo "Nope, nothing like that.";
}
echo "<h2>Refined Results:</h2>\n";
echo "<form name=refine onSubmit='return checkOmit();' method=post>\n";
echo "<input type=hidden name=query value=\"$query\"><br>";
echo "<input type=hidden name=refine value=true>\n";
echo "<input type=hidden name=to_omit value='$to_omit'>\n";
echo "<input type=hidden name=save value='save'>\n";
echo "<table><tr><td background='images/bg-menu.gif'valign=top>\n";
include "menu.php";
echo "</td><td>\n";
echo "<table align=center width=95%>\n";
echo "<tr
bgcolor=silver><td>Thumbnail</td><td>Address</td><td>Contact</td></tr>\n";
$c = 0;
while (($row = mysql_fetch_array ($res)) && $numrows > 0) {
$c++;
$id = $row[id];
$loc_name = $row[loc_name];
$folder = $row[folder];
$thumb = $row[thumb];
$address = $row[address];
$city = $row[city];
$state = $row[state];
$zip = $row[zip];
$contact_id = $row[contact_id];
$name = $row[name];
if(strlen($description) > 150) {
$description = substr($row[description], 0, 150).'...';
}
$type = $row[type];
if ($c % 2 == 0) {
$bg = "bgcolor=lightgrey";
} else {
$bg = 'bgcolor=white';
}
echo "<tr $bg>\n
<td><input type=checkbox name=omit value=$id>Omit<br>\n
<a href='$PHP_SELF?id=$id'><img src='$folder/thumbs/$thumb'></a></td>\n
<td>$loc_name<br>$address<br>$city, $state $zip</td>\n
<td>$name</td></tr>";
}
echo "<tr bgcolor=black><td colspan=3><font color=red>";
//debug:
//echo "Count of results: $numrows, contents of to_omit: $to_omit";
echo "Count of results: $numrows";
echo "</font></td></tr>
<tr><td colspan=3><input type=submit name=submit value=refine>
<input type=reset name=reset value=reset></td></tr>
</table>
</td></tr></table>
</form>";
//debug:
//echo $query;
} elseif ($id > 0){ // end refine, here begins detail record bookmark
$query = "select * from locations, contact
where contact_id = contact.id and locations.id = '$id'";
//debug:
//echo "$query<br><br>";
$res = mysql_query ($query, $db)
or die ("No selection, number: ".mysql_errno().", ". mysql_error());
$row = mysql_fetch_array($res);
/* //debug:
foreach($row as $k => $v){
echo "k: $k, v: $v<br>";
}
*/
$location_id = $row[0];
$loc_name = $row[loc_name];
$rci = $row[rci];
$folder = $row[folder];
$thumb = $row[thumb];
$type = $row[type];
$description = $row[description];
$rooms = $row[rooms];
$kw = $row[kw];
$special = $row[special];
$address = $row[10];
$city = $row[11];
$state = $row[12];
$zip = $row[13];
$contact_id = $row[contact_id];
$id = $row[15];
$name = $row[name];
$c_address = $row[address];
$c_city = $row[city];
$c_state = $row[state];
$c_zip = $row[zip];
$c_phone = $row[phone];
$email = $row[email];
echo "<table width=95% align=center><tr><td valign=top bgcolor=lightgrey>
<b>id:</b> $location_id<br>\n
<b>Thumbnail:</b> <br>
<a href='/locations/site/$folder/' target=ass>
<img src='/locations/site/$folder/thumbs/hero.jpg' heigth='120' width=160'
alt='Click to
open new window with links to large images'>
<br>$loc_name </a><br><font size=1>Click to open images in new
window</font><br><br>\n
<b>Site Name:</b> $loc_name <br>\n
<b>Address:</b> $address <br>\n
<b>City:</b> $city, $state $zip<br><br>\n
<b>Zone(rci):</b> $rci<br>\n
<b>Type:</b> $type<br>\n
<b>Description:</b> $description<br>\n
<b>Rooms:</b> $rooms<br>\n
<b>Keywords:</b> $kw<br>\n
<b>Special:</b> $special<br>\n
<br><br>
<b>Contact:</b><br>
Name: $name<br>\n
Address: $c_address<br>\n
$c_city, $c_state $c_zip<br><br>\n
<b>Phone:</b> $c_phone<br>
<b>Email:</b> $email<br>
<a href='http://maps.yahoo.com?addr=".urlencode("$address")."&csv=$zip'>Driving
Directions</a><br>\n
</td><td valign=top>";
##start inc.menu for detail display
$dio = opendir("$folder/thumbs");
$i=0;
while ($file = readdir($dio)){
if(is_dir($file)==false && substr($file, 0, 4) != "inc." &&
substr($file, 0, 1) !=
'.'){
$farray[$i] = $file;
$i++;
}
}
if (is_array($farray)==true) {
sort($farray);
}
for ($j = 0; $j < $i; $j++){
if (substr($farray[$j], -4) == '.htm') {
echo "<a href='$folder/thumbs/$farray[$j]'>\n";
echo "<img src='$folder/thumbs/".substr($farray[$j], 0, -4)."'
border=0></a>";
}
}
closedir($dio);
## end inc.menu stuff
echo "</td></tr></table>";
} elseif ($search) { //end detail begin main search form handling, generate query
based on input
bookmark
$query = "select locations.id, loc_name, locations.thumb, locations.folder,
locations.address, locations.city, locations.state, locations.zip,
contact.name from locations, contact
where locations.contact_id = contact.id AND \n";
if ($rci) {
$query .= "rci = '$rci' AND \n";
}
if ($type) {
$query .= "type like '%$type%' AND \n";
}
if ($description) {
$query .= "description like '%$description%' AND \n";
}
if ($rooms) {
$query .= "rooms like '%$rooms%' AND \n";
}
if ($kw) {
$query .= "kw like '%$kw%' AND \n";
}
if ($address) {
$query .= "locations.address like '%$address%' AND \n";
}
if ($city) {
$query .= "locations.city like '%$city%' AND \n";
}
if ($state) {
$query .= "locations.state like '%$state%' AND \n";
}
if ($zip) {
$query .= "locations.zip like '$zip%' AND \n";
}
if ($contact_id) {
$query .= "contact_id = '$contact_id' AND \n";
}
$query = chop($query); //chop off newline
$query = substr_replace($query, "", -3, 3); //chop off AND
$query .= " order by locations.id";
/* //debug:
foreach ($HTTP_POST_VARS as $k => $v){
echo "k: $k, v: <pre>$v</pre>";
}
*/
// echo "<h2>Query:</h2><pre>\"".$query."\"</pre><br>";
$res = mysql_query ($query, $db)
or die ("No quyery comrade, number: ".mysql_errno().", ". mysql_error());
//hack to make the single quote stay in hte query
$query = str_replace("'", "~", $query);
$numrows = mysql_numrows($res);
if($numrows < 1) {
echo "Nope, nothing like that.";
}
//gonna try a form here to find a set from out of a set.
echo "<h2>Results:</h2>\n";
echo "<table width=95%><tr><td background='images/bg-menu.gif'valign=top
width=150>\n";
include "menu.php";
echo "</td><td>\n";
echo "<form name=refine action=$PHP_SELF onSubmit='return checkOmit();'
method=post>\n";
echo "<table align=center width=100%>\n";
echo "<input type=hidden name=refine value=true>\n";
echo "<input type=hidden name=query value=$query>\n";
echo "<input type=hidden name=to_omit value=''>\n";
echo "<tr
bgcolor=silver><td>Thumbnail</td><td>Address</td><td>Contact</td></tr>\n";
$c = 0;
while (($row = mysql_fetch_array ($res)) && $numrows > 0) {
$c++;
$id = $row[id];
$loc_name = $row[loc_name];
$folder = $row[folder];
$thumb = $row[thumb];
$address = $row[address];
$city = $row[city];
$state = $row[state];
$zip = $row[zip];
$contact_id = $row[contact_id];
$name = $row[name];
if(strlen($description) > 150) {
$description = substr($row[description], 0, 150).'...';
}
$type = $row[type];
if ($c % 2 == 0) {
$bg = "bgcolor=lightgrey";
} else {
$bg = 'bgcolor=white';
}
echo "<tr $bg>\n
<td><input type=checkbox name=omit value=$id>Omit<br>
<a href='$PHP_SELF?id=$id'><img src='$folder/thumbs/$thumb'></a></td>\n
<td>$loc_name<br>$address<br>$city, $state $zip</td>\n
<td>$name</td></tr>";
}
echo "<tr bgcolor=black><td colspan=4><font color=red>";
echo "Count of results: $numrows, contents of to_omit: $to_omit";
echo "</font></td></tr>
<tr><td colspan=4><input type=submit name=submit value=refine>
<input type=reset name=reset value=reset></td></tr>
</table><input type=hidden name=query value='$query'></form>";
echo "</td></tr>";
//debug:
// echo "<h2>Query:</h2><pre>$query</pre><br>";
} else { //end form handling, below is main search form bookmark
//debug:
//echo $query;
?>
<h2>Start Page - Search Interface:</h2>
Intended for in-house use at location library.<br>
<font color=white>Click Submit to find all records, or enter search terms in the
boxes.</font>
<form name="theForm" method=post action="<?php echo $PHP_SELF; ?>">
<input type=hidden name=query value='<?php echo $query; ?>'>
<input type=hidden name=search value=true>
<div align="center">
<div align="center">
<table width="450" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="152" bgcolor="#C9BB7B" valign="top"
background="images/bg-menu.gif">
<p>
<a
href='javascript:document.theForm.submit(document.theForm.rci.value="");'>All</a></p>
<p><a
href='javascript:document.theForm.submit(document.theForm.rci.value="Intersection");'>Intersection</a></p>
<p><a href='admin/'>Admin</a></p>
<p><a
href='javascript:document.theForm.submit(document.theForm.rci.value="Residential");'>Residential</a></p>
<p><a
href='javascript:document.theForm.submit(document.theForm.rci.value="Commercial");'>Commercial</a></p>
<p><a
href='javascript:document.theForm.submit(document.theForm.rci.value="Industrial");'>Industrial</a></p>
<p><a href='docs/'>Help/Documentation</a></p>
<p> </p>
</td>
<td width="432" bgcolor="#FFCC66">
<table width="394" border="0" align="center">
<tr>
<td align="right">Zone (rci):</td>
<td>
<select name="rci">
<option value=''>Select Type</option>
<?php
$res = mysql_query("select distinct rci from locations", $db);
while ($myrow = mysql_fetch_array($res)) {
echo "<option value=\"$myrow[0]\"";
if($rci=="$myrow[0]") echo " selected";
echo ">".$myrow[0]."</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td align="right">Type:</td>
<td>
<input type="text" name="type" value="<?php echo $row[type]; ?>">
</td>
</tr>
<tr>
<td align="right" height="25">Description:</td>
<td height="25">
<input type="text" name="description" value="<?php echo
$row[description]; ?>">
</td>
</tr>
<tr>
<td align="right">Rooms:</td>
<td>
<input type="text" name="rooms" value="<?php echo $row[rooms]; ?>">
</td>
</tr>
<tr>
<td align="right">Keywords
(kw):</td>
<td> <b>
<input type="text" name="kw" value="<?php echo $row[kw]; ?>">
</b> </td>
</tr>
<tr>
<td align="right">Address:</td>
<td>
<input type="text" name="address" value="<?php echo $row[address];
?>">
</td>
</tr>
<tr>
<td align="right">City:</td>
<td>
<input type="text" name="city" value="<?php echo $row[city]; ?>">
</td>
</tr>
<tr>
<td align="right">State:</td>
<td>
<input type="text" name="state" value="<?php echo $row[state]; ?>">
</td>
</tr>
<tr>
<td align="right">Zip:</td>
<td>
<input type="text" name="zip" value="<?php echo $row[zip]; ?>">
</td>
</tr>
<tr>
<td align="right">Contact_id
(id number from contact table):</td>
<td>
<input type="text" name="contact_id" value="<?php echo
$row[contact_id]; ?>">
</td>
</tr>
<tr>
<td align="right">phone:</td>
<td>
<input type="text" name="phone" value="<?php echo $row[phone]; ?>">
</td>
</tr>
<tr>
<td align="right">Email:</td>
<td>
<input type="text" name="email" value="<?php echo $row[email]; ?>">
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</div>
<p align="center"><br>
<input type=submit value=submit name=go>
<input type=reset name=reset value=Clear>
</p>
</form>
<?php
}
?>
</body>
</html>
end script
TIA,
t
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]