Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Frustrated beginner 1

Status
Not open for further replies.

beeg

Programmer
Jun 10, 2003
13
US
PHP question; I know this is alot, but I'm desperate.
This script actually calls the whole table rather then meeting the search parameters. Can someone help with altering the php script to actually do what I want it to, find matches for searchtype and searchterm?

Thanks - Desperate

This is from form.htm

<form action=&quot;results.php&quot; method=&quot;post&quot;>
<p>Search by:
<select name=&quot;searchtype&quot;>
<option value=&quot;name&quot;>Organization Name
<option value=&quot;denomination&quot;>Denomination
<option value=&quot;city&quot;>City
</select>
Keyword:
<input type=&quot;text&quot; name=&quot;searchterm&quot;>
<input type=&quot;submit&quot; value=&quot;Search&quot;>
</p>
</form>


This is from results.php

//feild names if you need them;
//name, pastor, denomination, address, city, state, zip, phone;
//If possible i would like 1st 3 in 1 TD and last 5 in 1 TD

<?php
trim($searchterm);
if (!$searchtype || !$searchterm)
{
echo &quot;<p align=center><b>You have not entered search details. Please go back and try again.</b></p>&quot;;
exit;
}
/* Connecting, selecting database */
@ $db = mysql_connect(&quot;db_host&quot;, &quot;db_user&quot;, &quot;db_pass&quot;)
or die(&quot;Could not connect&quot;);
mysql_select_db(&quot;churches&quot;) or die(&quot;Could not select database&quot;);
/* Performing SQL query */
$query = &quot;SELECT * FROM church order by 'name' asc&quot;;
$result = mysql_query($query) or die(&quot;Query failed&quot;);
$num_results = mysql_num_rows($result);
print &quot;<p><b>Your search produced &quot;.$num_results.&quot; matches.</b></p>&quot;;
/* Printing results in HTML */
print &quot;<table border=1 cellspacing=0 cellpadding=2 width=750 align=center>\n&quot;;
print &quot;<tr>\n&quot;;
print &quot;<td bgcolor=#0099CC colspan=3><p><b>Organization</b></p></td>\n&quot;;
print &quot;<td bgcolor=#0099CC colspan=5><p><b>Information</b></p></td>\n&quot;;
print &quot;</tr>\n&quot;;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($searchterm = '%&quot;nameof&quot;%');
print &quot;<tr>\n&quot;;
foreach ($line as $col_value) {
print &quot;\t\t<td height=30><p><font size=1>$col_value</font></p></td>\n&quot;;
}
print &quot;\t</tr>\n&quot;;
}
print &quot;</table>\n&quot;;
/* Free resultset */
mysql_free_result($result);

/* Closing connection */
mysql_close($db);
?>
 
Change
Code:
   $query = &quot;SELECT * FROM church order by 'name' asc&quot;;
to
Code:
   $query = &quot;SELECT * FROM church WHERE &quot;.$searchtype.&quot; LIKE '%&quot;.$searchterm.&quot;%'order by 'name' asc&quot;;
This would only select the rows that matches the search criterias. //Daniel
 
Okay, tried that and got back query failed.
Thanks - Beeg
 
Oh!! it worked thank you so much!
Last thing do you know how to change it so name, pastor, denomination feilds will be in one TD and address, city, state, zip, phone will be in one TD.

Beeg
 
This should do it.
Code:
    while ($row = mysql_fetch_assoc($result)) {
        print &quot;<tr>\n&quot;;
        printf(&quot;<td colspan=\&quot;3\&quot;>%s, %s, %s</td><td colspan=\&quot;5\&quot;>%s, %s, %s, %s, %s</td>\n&quot;, $row['name'], $row['pastor'], $row['denomination'], $row['address'], $row['city'], $row['state'], $row['zip'], $row['phone']);
        print &quot;\t</tr>\n&quot;;
    }
You could also just use the concatentation operator to create the string. //Daniel
 
close table looks right, but have 3 ,,, in 1st TD and 5 ,,,,, in the next. Didn't put the information in.

Thanks - Beeg
 
Help; almost works but got 2 commas in first TD an 4 in second but don't get the information.
Beeg
 
What is the script you're using? Do you have a empty row in your database? //Daniel
 
Not that I'm aware of. what do you mean by script
 
Is it just the last row that doesn't work for you or is it all rows? //Daniel
 
all the rows, so for example is says there are 5 results blah blah
then the table starts top row has the headers i placed org and info. But all subsequent rows only have the commas

Beeg
 
Also I was going to vote for you to express my gratitude for your help thus far and can't find the voting spot??

Beeg
 
It's because you are a visitor. Visitors cannot vote.
Do what sleipnir214 told you to do in your other thread, which was adding a
Code:
print_r($row);
right after the
Code:
while ($row = mysql_fetch_assoc($result)) {
The whole while loop would then look like this:
[tt] while ($row = mysql_fetch_assoc($result)) {
print_r($row);
print &quot;<tr>\n&quot;;
printf(&quot;<td colspan=\&quot;3\&quot;>%s, %s, %s</td><td colspan=\&quot;5\&quot;>%s, %s, %s, %s, %s</td>\n&quot;, $row['name'], $row['pastor'], $row['denomination'], $row['address'], $row['city'], $row['state'], $row['zip'], $row['phone']);
print &quot;\t</tr>\n&quot;;
}[/tt]
Post the results of this here, it would help a lot. //Daniel
 
Signed up last night, so I see what you're talking about. Figured I would finish this thread as Beeg.
Anyway result was it printed everythin like this;

Array ( [Name] => Anchor Baptist Church [Pastor] => Pastor David Foster [Denomination] => Baptist [Address] => 3524 N.E. 95th Street [City] => Seattle [State] => WA [Zip] => 98115 [Phone] => 206-525-5646 )

This is one result, so if it said 7 I got seven of these. All printed above the table, not in it, table still has the commas though

Beeg
 
Since PHP variables are case sensitive, you should use
Code:
        printf(&quot;<td colspan=\&quot;3\&quot;>%s, %s, %s</td><td colspan=\&quot;5\&quot;>%s, %s, %s, %s, %s</td>\n&quot;, $row['Name'], $row['Pastor'], $row['Denomination'], $row['Address'], $row['City'], $row['State'], $row['Zip'], $row['Phone']);
//Daniel
 
or don't use any row names and let the PHP make the table to fit.:
---------------------------------------------------------
<?php

trim($searchterm);

if (!$searchtype || !$searchterm) {

echo &quot;<p align=center><b>You have not entered search details. Please go back and try again.</b></p>&quot;;

exit;

}

// connect to database

$db = mysql_connect(&quot;db_host&quot;, &quot;db_user&quot;, &quot;db_pass&quot;);

mysql_checkerror();

mysql_select_db(&quot;churches&quot;);

mysql_checkerror();

// do the query

$query = &quot;SELECT * FROM church WHERE &quot;.$searchtype.&quot; LIKE '%&quot;.$searchterm.&quot;%'order by 'name' asc&quot;;

$result = mysql_query($query);

mysql_checkerror();

// get number of results

$num_results = mysql_num_rows($result);

print &quot;<p><b>Your search produced &quot;.$num_results.&quot; matches.</b></p>&quot;;

// figure out how many columns are in it and do the table to fit.

$columns=@mysql_num_fields($result);

echo &quot;<table align=\&quot;center\&quot; border=\&quot;0\&quot; cellspacing=\&quot;1\&quot; cellpadding=\&quot;0\&quot;>\n&quot;;

$row=0;

// display field names set to 0 for no field names

$display=1;

if($display == 1){
print &quot;<th>row</th>&quot;;

for ($i = 0; $i < mysql_num_fields($result); $i++) {

print &quot;<th>&quot;.preg_replace(&quot;/_/&quot;,&quot; &quot;,mysql_field_name($result,$i)).&quot;</th>\n&quot;;

}
}
while ($myrow = @mysql_fetch_array($result)){

// start the results display table, display alternate rows in different colors.

$row++;

if($row%2){

$color=&quot;#CCCCCC&quot;;

}else{

$color=&quot;#9FC6C4&quot;;

}

echo &quot;<tr><td>$row</td>&quot;;

for ($i = 0; $i < ($columns); $i++) {

echo &quot;<td bgcolor=\&quot;$color\&quot;> $myrow[$i] </td>\n&quot;;

}

echo &quot;</tr>\n&quot;;

}

mysql_free_result($result);



echo &quot;</table>\n&quot;;

// functions

function mysql_checkerror(){

$err_no=mysql_errno();

if ($err_no > 0 ){

echo &quot;<h2> Error:</h1> &quot; . mysql_errno() . &quot;: &quot; . mysql_error() . &quot;<br>\n&quot;;

exit;

}
}

?> ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Karver thanks for the input, part of the issue was that in output I wanted to combine certain fields, so we couldn't do that.

Daniel - Thanks for all your help and patience, that last one worked. I hadn't realized I capitalized the fields I almost never use upper case when coding.

Beeg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top