Okay, so as some of my other threads have shown, I'm not especially familiar with PHP, except for the basics. I can do the easy stuff - make a form, set inputted data as variables, search an existing database for matches, and outputting the resulting rows in tabular format. The way I've designed my setup is to have 3 files - the first is the web page with the form itself. The second is the PHP "engine" (it takes the data, sets variables, and searches). The third is the results page, where I'd like to have the search results posted. I set it up like this because I have a basic template where the content is dynamically loaded into a "content" area when requested. So, really, the index page acts as the backbone, and the meat is loaded up when necessary. It also keeps me from having to write a bunch of "echo" statements within the PHP, which seems tedious when the backbone is already made.I assume most people do it like this, but maybe not - if you have other suggestions, please feel free to suggest them.Anyway, assuming this is a good way to do it, what would be the best way (in your more educated opinions) to pass the search results from the PHP "engine" page to the results page (which is essentially the template with a "content" area where the results will display)?Thanks in advance for the suggestions (BTW, this is for the same non-profit I posted about a while back, so, while you are sort of doing my job for me, you're helping a good cause and we all appreciate the help )![Edited on March 27, 2008 at 4:30 PM. Reason : spelling]
3/27/2008 4:29:22 PM
Keep it all in one page or use includes ... using an if or case statement to determine which content or include you want displayed.Now you have all the passed/parsed/queried variables in one place.
3/27/2008 4:33:43 PM
You're saying you want to condense your three files into one file? Because that would certainly be the simplest, most efficient way to do this.A very crude way to do it, passing variable $foo via url:
<?php // if variable(s) is/are not passed, display form if (!$foo) { echo "<form action=\"$_SERVER['PHP_SELF']\" method=\"post\">"; echo "<input type=\"text\" name=\"foo\">"; echo "<input type=\"submit\" value=\"Submit Form\">"; echo "</form>"; } // if variable(s) is/are passed via url, fetch and display data elseif ($foo) { $query = "SELECT blah FROM crap"; while($row = mysql_fetch_row($query)) { $row[0] = $bar echo $bar . "<br />"; } }?>
3/27/2008 4:41:50 PM
Okay, I may not have explained myself very well (plus, it's hard to describe what I've got without showing you) - my bad. I'm just putting down enough code to get the point across, and then doing this from memory since I don't have the files with me right now, so bear with me if there's something basic missing. It really does work, I promise.-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+INDEX.PHP (index.php?p=search displays the correct content - the form - through a case)
<html><body><form method="post" action="search.php">First Name: <input type="text" id="first_name" name="first_name" /><input type="submit" id="search" name="search" value="Search" /></form></body></html>
3/27/2008 5:29:02 PM
Double post - I don't know why.[Edited on March 27, 2008 at 5:30 PM. Reason : ?]
3/27/2008 5:29:37 PM
I'm pretty sure the code I posted does exactly what you're looking for.
3/27/2008 5:36:05 PM
Ah, actually, I see your code a bit differently than I originally did. That actually makes more sense, to have the PHP echo the form code instead. I'll give that a shot - thanks for the suggestion!As for your comment, I think I explained badly (again) what I meant to say. HTML and CSS I'm very familiar with - but I think I kept looking at this as an attempt to make it more "dynamic" than it needed to be (or, at least, in the wrong way). Again, thanks for the suggestion.
3/27/2008 5:41:04 PM
Alternately, as I said ... instead of putting the echos and queries, etc that Ernie posted inside the if(), you can put them into their own seperate files and then just include(somefile.php); ... this will allow you to reuse/include that particular code on various pages and just change it once. It works especially well with regard to queries.Google "php include" if what I said doesn't make sense.[Edited on March 27, 2008 at 5:48 PM. Reason : .]
3/27/2008 5:47:42 PM
gs7 and Ernie are both right!
3/27/2008 5:53:04 PM
gs7 and Ernie and quagmire02 are all right!
3/27/2008 6:19:48 PM
you might find the extract() function useful: http://us.php.net/extract
3/27/2008 10:38:01 PM
Okay, I have yet another question that I'm hoping you guys can help me with. I took the advice from above, and you guys were right - it worked well. Now, though, I'm not sure how to rearrange information that I'm pulling out of the Oracle database. The database contains 14 different columns (and who knows how many rows as unique entries), but I'm only concerned with showing 5 of these columns for the records that are results of the search. Now, as you can see below, it goes through the entire database, outputs the results by row, and displays them as a table - this works just fine (I changed the variables a bit for this demonstration, so if there are some inconsistencies, I apologize - otherwise, this is nearly the entire working script).
<?php// includes the database connection fileinclude("connect.php");// begin search functions if received form submissionif (isset($_POST['search'])) { // if the field is empty if (empty($_POST['lastname'])) { echo "you forgot to fill in a field"; exit; } // assign form fields as variables $lastname= $_POST['lastname']; // results - strip html, trim characters $lastname = strip_tags($lastname, "<br />"); $lastname = trim($lastname); // actual search for inputted terms $query = " SELECT DONORS,HONOREES,MEMORIALS,DON_TEXT,PICTURE FROM monument_db1 WHERE upper(HONOREES) LIKE upper('%$lastname%') ORDER BY DONORS ASC"; // parsing the query into a statement of results $statement = oci_parse($connect, $query); oci_execute($statement); // searches and displays the entire row associated with search results $rows = oci_fetch_all($statement, $results); if ($rows > 0) { echo "Your search for <em>" .$lastname. "</em> yielded <em>" .$rows. "</em> results:<br /><br />"; echo "<table id=\"show_results\" cellpadding=\"0\" cellspacing=\"0\" border=\"1\">\n"; echo "<tr>\n"; foreach ($results as $key => $val) { echo "<th>$key</th>\n"; } echo "</tr>\n"; for ($i = 0; $i < $rows; $i++) { echo "<tr>\n"; foreach ($results as $data) { echo "<td>$data[$i]</td>\n"; } echo "</tr>\n"; } echo "</table>\n"; } else { echo "No data found!<br />\n"; } // releases resources commited by oci_parse and/or oci_execute oci_free_statement($statement);}// close the database connectionoci_close($connect);?>
3/31/2008 3:57:21 PM
I'm not sure I fully understand your question.
3/31/2008 5:28:17 PM
^ Okay, yeah, I did a bad job of explaining it. You actually brought my point, though - I don't want the big block of text. I want to pull up the full record (all 5 columns) that matched the search, and then arrange the data regardless of how it's organized in the database - right now, DONORS is the first column, HONOREES is the second column, and so on. I want to be able to take the individual result (of which there might be 5 or 10 or however many that matched the search criteria) and do something like:
echo "<p>Honoree's Name: ".$data[2]."</p>";echo "<p>Donation Text: ".$data[4]."</p>";echo "<p>Donor's Name: ".$data[1]."</p>";echo "<p>Memorial Title: ".$data[3]."</p>";echo "<img src=".$data[5]." alt=".$data[3]." border=\"0\" />";
3/31/2008 7:54:17 PM
What you want is something along the lines of:
$statement = oci_parse($connect, $query);oci_execute($statement);while ($row = oci_fetch_assoc($statement)) { echo "Donors: $row['DONORS']<br/>"; echo "Honorees: $row['HONOREES']<br/>"; ... //etc etc}
3/31/2008 8:23:04 PM
i understand that this server was donated, but why would anyone run oracle as opposed to something free like mysql? this isn't me being a douche - i'm curious as to the advantages of paying for the database, rather than getting one for free...especially since mysql has such a large user base and has been around for a while (it's not like some new thing where only 5 people in the world use it)
4/1/2008 2:37:59 PM
And it's a hell of a lot easier to deal with.
4/1/2008 2:48:52 PM
I've always been told Oracle is much better when you start dealing with massive databases that need to be queried millions of times a day (think online stock brokers, credit card transactions, etc.). Oracle also comes with tons of support services. And paying for Oracle means that if it craps out you can blame/sue Oracle. If MySQL craps out your just SOL.[Edited on April 1, 2008 at 3:31 PM. Reason : oh and.... http://www.google.com/search?hl=en&q=oracle+vs+mysql&meta=]
4/1/2008 3:17:04 PM
^^ which is?^ yeah, that's what i figure...if you're a big company, and your data is at stake, i guess throwing money at oracle is a good way to assure that your database is robust and supported 24/7 by the people who built itbut otherwise, unless you're a large, rich company, i can't see any reason to pay for something like oracle
4/1/2008 4:09:10 PM
4/1/2008 4:13:54 PM
mysql is easier, IMHO.
4/1/2008 4:23:49 PM
I was not going to get into the semantics for which DB to use, but since it's being discussed ... unless you have a very very compelling reason to stay with Oracle, swap to MySQL, you'll thank yourself.
4/1/2008 5:54:43 PM
Okay, so far everything's going very well, and thank you all for your input. I am, however, running into an issue with a SQL query. It looks like this:
SELECT * FROM $tableWHERE upper(FNAME) LIKE upper('%$fname%') OR upper(LNAME) LIKE upper('%$lname%') AND DON_LVL='gold'ORDER BY LNAME DESC";
4/8/2008 5:29:45 PM
SELECT DISTINCT * FROM $tableWHERE (upper(FNAME) LIKE upper('%$fname%') AND DON_LVL='gold') OR (upper(LNAME) LIKE upper('%$lname%') AND DON_LVL='gold')ORDER BY LNAME DESC";
4/8/2008 5:39:20 PM
Like ^said, use the parenthesis, they are your friend for grouping different sets of ORs in a query.
4/8/2008 5:55:28 PM
^^ That did it...thanks![Edited on April 9, 2008 at 9:52 AM. Reason : .]
4/9/2008 9:39:51 AM
I'm using the PHP module (or library) that allows the server to resize uploaded images, which is great. My next goal is to allow someone, from a web interface, to replace an existing image with another one. So, for example, they pull up database entry for one of our city projects, which has an image as part of its entry. Sometimes, though, a volunteer will bring in a better image, and our office manager would like to be able to replace the existing image with the new one. I assume that the easiest way to do this is just to have the upload field check to see if the image file name already exists (which it will), and then overwrite it? That seems relatively easy, but how do I have the online form ask for permission to overwrite the existing image? Some JavaScript?Also, when I bring up a database entry, I don't simply have it display the contents - instead, I have it show up in a form where the entries are displayed within text fields so they can be edited:
<input type="text" value="<?php echo $fname; ? />
4/14/2008 5:11:48 PM
Check for permissions from a database field before you render the page (or element), then when it gets to the specific form, do an integrated if/else statement to show or not show the upload image form.
4/15/2008 12:07:47 AM
^ I don't think I quite understand what you're saying (but it's probably because I'm still pretty new to all of this). Right now, as it stands, there are two different types of pages for displaying records - the one that the public sees, which just prints out the information and the second is an administrative page, where someone who logs sees the same information, but they're displayed in form fields, so that they can change them if they want. Currently, if the database entry contains a picture, it just says something like "Image: Yes" and the "Yes" is a link to the image. If there's no picture, it displays the file upload input. I did it like this because I couldn't get the image path to display in the file upload text box. Why would the other input boxes be able to access and display the information, but not the file upload field?Either way, that's not strictly necessary - it was just a quirk I came upon. What I meant to say about asking permission was what would happen in this scenario:1.) Admin pulls up record and image already exists2.) Admin chooses another image from their hard drive and selects "upload"3.) A message comes up, asking if they want to overwrite the old file4.) Yes takes them back to the record page with the new image, No takes them back to the record page with the old image still thereIs this more complicated than it needs to be? I'm pretty sure I can easily just delete the old image and upload the new, but I was hoping to insert some sort of last-minute confirmation, just in case. Is it not worth the extra effort?Okay, second question (and again, I really appreciate all of the help you guys have provided!) - Some of this data comes from an OLD MicrosoftSQL database, and the way it was designed was to have certain characters inserted in order to display the results in a certain way. For example, when a user put in a ; it would drop everything after that to a new line (so if you have 2 donors for a project, you'd put in "Smith, John; Smith, Jane" and it would display each donor on their own line). Also, the old system actually required the user to put in \ before using quotation marks or apostrophes (I'm not sure why, but now the actual data has these backslashes in them. I'd like to convert the ; into newlines (or line feeds?) and I'd like to strip the slashes out of the data when it's displayed. I've come across ereg_replace(), preg_replace(), str_replace(), strip_tags(), and stripslashes(), but I haven't had any success (I'm using the line below as my test). ANY help you can provide would be welcome - I think it's a pretty simple thing to do, I just don't know where to start.
$string = "Hello, all; my name is \"Jonathan\" or \'Jon\' for short.";
Hello, allmy name is Jonathan or 'Jon' for short.
4/16/2008 10:01:05 AM
$newstring = str_replace(";", "\n", $string);
4/16/2008 10:22:55 AM
^ That sort of worked. The "\n" didn't drop the line, but throwing a "<br />" did it. I tried that earlier, but stupidly forgot to assign the output as a variable. Any suggestions for removing the backslashes? When I try to do this:
$string = str_replace("\","",$string);
4/16/2008 10:57:10 AM
$newstring = stripslashes($string);
4/16/2008 11:04:09 AM
I could have SWORN that I did exactly that. It's pretty straight-forward. And yet, putting that in there makes it work. I'm having a horribly n00b day today. Thanks for the information, I appreciate it!
4/16/2008 11:17:59 AM
Heh, this is how you learn. All the questions you've been asking reminded me and everyone else that's been helpful in here how it was for them when they first got into it.It's always some dumb little thing or something you KNOW you'd already tried...
4/16/2008 11:27:35 AM
yeah, don't sweat it. everybody goes through it. even i still kick myself for making this thread message_topic.aspx?topic=383993
4/16/2008 11:29:12 AM
LOL I remember that thread.What a n00b!
4/16/2008 11:33:13 AM
4/16/2008 11:35:51 AM
Yeah, I definitely appreciate everything. I'm getting there!Anyway, since everyone's being so helpful, I guess I'll just keep asking for help: in the old database, names are entered as "lastname, first name". For display purposes, how might I switch those two around and eliminate the comma? Also, in cases where it might say "lastname, husbandname, wifename", how would I do that? It's safe to assume that the first comma in the string is always after the last name, but that if there's a second comma, it will refer to another person's first name. So, in the case where it's entered as
lastname, husbandname, wifename
husbandname & wifename lastname
4/16/2008 11:49:23 AM
list($last, $first, $wife) = explode(", ", $str);print $first . ($wife ? " & $wife " : " ") . $last;[Edited on April 16, 2008 at 12:02 PM. Reason : use split() if you need regex]
4/16/2008 11:57:47 AM
preg_match_all("#([^,]+)#", $str, $match);echo "{$match[1][1]} ".(($match[1][2])?"& {$match[1][2]}":"")." {$match[1][0]}";
4/16/2008 1:45:25 PM
what's the advantage of ^^ over ^ and vice versa?
4/16/2008 4:19:14 PM
in terms of resourcesexplode < split < preg_match < preg_match_alland i think mine is easier to read
4/16/2008 4:28:01 PM
The explode method is (for me) easier to read and, since it was also first, I went with that one...it appears to be working flawlessly so far. I have one more question, but I'm hopefully nearly done with this particular project, so this might be the last one. Maybe. Anyway, I have "previous" and "next" links on each page that displays a record. I made them by doing this:
$rcquery = "SELECT COUNT(*) AS num_rows FROM $table";$rcstmt = oci_parse($conn, $rcquery);oci_define_by_name($rcstmt,"NUM_ROWS",$num_rows);oci_execute($rcstmt);oci_fetch($rcstmt);echo "<div class=\"nav\"><ul>";if ($rid > 1) { echo "<li><a href=\"index.php?p=rec&rid=".($rid-1)."\">previous record</a></li> | ";}if ($rid < $num_rows) { echo "<li><a href=\"index.php?p=rec&rid=".($rid+1)."\">next record</a></li>";}echo "</ul></div>";
4/17/2008 9:58:55 AM
In your select statement, only select rows that have data in them.
4/17/2008 10:57:13 AM
^ I don't think that worked. Well, more than likely it just means I didn't do it correctly. I changed the select statement to:
$rcquery = "SELECT COUNT(*) AS num_rows FROM $table WHERE PROJECT_NAME IS NOT NULL";
4/17/2008 11:47:19 AM
Your $rid-1 and $rid+1 will be problematic.I'd just have it go by the rid of the next row. Do a seperate select statement for the next and previous link's rids. For the next one, use a LIMIT on your select of only 1 row, and then specify it has to be greater than the current record's rid and sort ascending.Then spit that out for the next record's rid.Do the same for the previous but specify it be smaller than the current id and sort descending.[Edited on April 17, 2008 at 12:07 PM. Reason : *]
4/17/2008 12:06:49 PM
I guess I only post on here when I have stupid questions. Anyway, thanks to everyone for their help - I think I'm doing pretty well so far, though I have yet another question. I'm sure the answer exists somewhere, but I don't know what to Google for in order to find it.We use Oracle, so I only know how to use PHP to connect to an Oracle database. This question is in regards to my own personal site (not the non-profit), so I'm using MySQL. This is what my Oracle connection script looks like:
<?php$user = "username";$pswd = "password";$host = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS =(PROTOCOL=TCP)(HOST=databasehost.com)(PORT=1234)))(CONNECT_DATA=(SID=databasename)))";$connect = oci_connect($user,$pswd,$host);if (!$connect) { echo "Connection failed!<br /><br />"; $error = OCIError(); echo "Error: $error[code] $error[message]"; exit;}$committed = oci_commit($connect);if (!$committed) { $error = oci_error($connect); echo "Commit failed. Oracle reports: " .$error['message'];}?>
<?php$user = "username";$pswd = "password";$host = "databasehost.com";$link = mysql_connect($host,$user,$pswd);if (!$link) { echo "Connection failed!<br /><br />"; echo "Error: " . $mysql_error(); exit;}?>
7/3/2008 9:26:26 PM
http://us3.php.net/manual/en/function.mysql-select-db.phpis that what you're asking?[Edited on July 3, 2008 at 9:50 PM. Reason : connecting to the server and then choosing a DB are two separate functions w/ MySQL]
7/3/2008 9:48:45 PM
ugh, 4 space tabs ftl
7/4/2008 5:30:54 AM
No, that's not quite what I'm asking. I mean, yes, that explains that with MySQL, the selection of the database is a two-step process and, while I didn't know that, my question was in regards to how I connect to the database server.All of the examples I've seen so far use "localhost" as their database server. But isn't that only valid if the PHP document is stored on the same server as the MySQL database server? Because in my case, that's not what's happening.For example, if my PHP documents are all on server 1 at blahblah.com and the MySQL database (let's call it "apples") is on server 2 foobar.com, how would I connect to it? Would it look like this (I fixed the spaces for Donogh5 ):
<?php$user = "username";$pswd = "password";$host = "foobar.com";$db = "apples";$link = mysql_connect($host,$user,$pswd);if (!$link) { echo "Connection failed!<br /><br />"; echo "Error: " . $mysql_error(); exit;}$db_connect = mysql_select_db($db,$link);if (!$db_connect) { die("Can\'t use this database: " . mysql_error());}?>
7/6/2008 5:08:05 PM