User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » PHP - form, search, passing results to new page Page [1] 2, Next  
vertigo
Veteran
135 Posts
user info
edit post

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

gs7
All American
2354 Posts
user info
edit post

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

Ernie
All American
45943 Posts
user info
edit post

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 />";
}
}
?>


[Edited on March 27, 2008 at 4:44 PM. Reason : ]

3/27/2008 4:41:50 PM

vertigo
Veteran
135 Posts
user info
edit post

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>


-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

SEARCH.PHP

This has nothing but PHP in it - no HTML per se (some echoes in the case of SQL errors or something, but that's it). Again, I don't have it with me, so this might be a pointless post, but it is essentially just a basic SQL search (we're using a server with Oracle, since it was donated) that scans each record and outputs the row (each row contains 5 columns with more specific information - we only show the user 3 of these, though). Currently, it just echoes a full table (since I wanted to test to make sure I had the queries down right, and it works fine). Currently, it only supports one type of search (first name) because I, again, just wanted to get it working before I continued, since this is new to me.

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

RESULTS.PHP

This page is a template, much like index.php - it contains the basics (HTML tags, CSS links, the entire "surrounding" code that formats the page under the same look as the rest of the pages). My goal is to have the results from search.php pass to this page and throw them for display. I really don't want them displayed as a table, but if that's easier, then no problem. I would assume (from very basic research) that throwing the results from search.php into an array and then displaying the array might be the best option, but I'm not sure how to go about doing this (or if there's a better way).

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Maybe I should explain why I decided to do it like this - I really like the idea of modular code. Being able to tweak something on the "template" and having it take effect immediately across all pages is great. If, later on down the road, we decide to revamp the whole thing and start from scratch, I want to be able to simply throw the PHP "engines" in there and just re-use them without much effort on my part. Also, as I work on several non-profit sites, I'd like to be able to just take those same PHP pages with me and use them on other sites without much modification. Does that make sense? Do you have any better suggestions?

If it would be better to actually provide you guys with the files themselves, I can do that - just not at the moment. Again, thanks for your help!

3/27/2008 5:29:02 PM

vertigo
Veteran
135 Posts
user info
edit post

Double post - I don't know why.

[Edited on March 27, 2008 at 5:30 PM. Reason : ?]

3/27/2008 5:29:37 PM

Ernie
All American
45943 Posts
user info
edit post

I'm pretty sure the code I posted does exactly what you're looking for.

Quote :
"Maybe I should explain why I decided to do it like this - I really like the idea of modular code. Being able to tweak something on the "template" and having it take effect immediately across all pages is great. If, later on down the road, we decide to revamp the whole thing and start from scratch, I want to be able to simply throw the PHP "engines" in there and just re-use them without much effort on my part. Also, as I work on several non-profit sites, I'd like to be able to just take those same PHP pages with me and use them on other sites without much modification. Does that make sense? Do you have any better suggestions?"


This has nothing to do with your PHP, very little to do with your HTML, and everything to do with your CSS. It isn't really related to the form issue.

3/27/2008 5:36:05 PM

vertigo
Veteran
135 Posts
user info
edit post

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

gs7
All American
2354 Posts
user info
edit post

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

quagmire02
All American
44225 Posts
user info
edit post

gs7 and Ernie are both right!

3/27/2008 5:53:04 PM

qntmfred
retired
40553 Posts
user info
edit post

gs7 and Ernie and quagmire02 are all right!

3/27/2008 6:19:48 PM

DirtyMonkey
All American
4269 Posts
user info
edit post

you might find the extract() function useful: http://us.php.net/extract

3/27/2008 10:38:01 PM

vertigo
Veteran
135 Posts
user info
edit post

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 file
include("connect.php");

// begin search functions if received form submission
if (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 connection
oci_close($connect);

?>


However, I want to be able to extract the individual cells and display them not as a table. The HTML/CSS part of it (and echoing it in the PHP script) is easy. What I'm not sure about is how to pull that information out individually, while retaining the linking (obviously, the columns in each row are related, and each column itself defines the context of the data - I'd like to preserve these associations). I'm sure this is pretty easy, but what I'm not sure about is how to go about doing it.

Again, any suggestions you have are greatly appreciated. I know I keep asking somewhat stupid questions, but I really appreciate your guidance!

3/31/2008 3:57:21 PM

Ernie
All American
45943 Posts
user info
edit post

I'm not sure I fully understand your question.

Quote :
"However, I want to be able to extract the individual cells and display them not as a table."


Then just remove the table tags. Of course it'll just be a block of text then, how do you want the information displayed?

3/31/2008 5:28:17 PM

vertigo
Veteran
135 Posts
user info
edit post

^ 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\" />";


Does that make sense? Right now, it just outputs the results exactly like you said - a block of text that's arranged as cells. How do I pull out those individual cells, assign them as variables, and then use those individual variables to output a specific way of displaying them? Also, the information contained in the PICTURE column is the location of the picture file - so I want to be able to call the picture that goes with honoree's name, donation text, donor's name, and memorial title. Is that a better explanation?

3/31/2008 7:54:17 PM

gs7
All American
2354 Posts
user info
edit post

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
}


The oci_fetch_assoc() aka, an associative fetch, returns an array with field names as the identifiers instead of numbers.

Fwiw, PHP maintains similar functions with other databases as well, so you can call mysql_fetch_assoc(), for example, and expect similar behavior.

[Edited on March 31, 2008 at 8:30 PM. Reason : .]

3/31/2008 8:23:04 PM

quagmire02
All American
44225 Posts
user info
edit post

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

DeltaBeta
All American
9417 Posts
user info
edit post

And it's a hell of a lot easier to deal with.

4/1/2008 2:48:52 PM

Talage
All American
5091 Posts
user info
edit post

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

quagmire02
All American
44225 Posts
user info
edit post

^^ 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 it

but 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

Ernie
All American
45943 Posts
user info
edit post

Quote :
"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."


That's mostly true, but this guy is designing a website for a soup kitchen

4/1/2008 4:13:54 PM

DeltaBeta
All American
9417 Posts
user info
edit post

mysql is easier, IMHO.

4/1/2008 4:23:49 PM

gs7
All American
2354 Posts
user info
edit post

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

vertigo
Veteran
135 Posts
user info
edit post

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 $table
WHERE upper(FNAME) LIKE upper('%$fname%') OR upper(LNAME) LIKE upper('%$lname%') AND DON_LVL='gold'
ORDER BY LNAME DESC";


So, essentially, you have a search field where you can input either the first name or last name of the donor, and then search those donors that are gold-level supporters. The problem I'm running into is that whenever I run the query, it returns all results in the database and ignore the first/last name inputs. When I remove the upper requirement (which I inserted to remove case sensitivity from the search), it works just fine. If I remove one of the options from the OR statement (so it's just AND), it works just fine with the upper conversion. I've also tried putting the entire OR statement into parentheses, but that doesn't work, either. Am I missing something? I think I've narrowed it down to an issue with using upper and OR, but maybe not. Suggestions? Thanks!

[Edited on April 8, 2008 at 5:30 PM. Reason : formatting]

4/8/2008 5:29:45 PM

BigMan157
no u
103352 Posts
user info
edit post

SELECT DISTINCT * FROM $table
WHERE (upper(FNAME) LIKE upper('%$fname%') AND DON_LVL='gold') OR (upper(LNAME) LIKE upper('%$lname%') AND DON_LVL='gold')
ORDER BY LNAME DESC";


maybe?

4/8/2008 5:39:20 PM

gs7
All American
2354 Posts
user info
edit post

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

vertigo
Veteran
135 Posts
user info
edit post

^^ That did it...thanks!

[Edited on April 9, 2008 at 9:52 AM. Reason : .]

4/9/2008 9:39:51 AM

vertigo
Veteran
135 Posts
user info
edit post

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; ? />


This works great for everything except the file (image) field. It doesn't matter what I put there, it doesn't show the path to or the file name itself. Suggestions?

Thanks for your help!

4/14/2008 5:11:48 PM

gs7
All American
2354 Posts
user info
edit post

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

vertigo
Veteran
135 Posts
user info
edit post

^ 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 exists
2.) Admin chooses another image from their hard drive and selects "upload"
3.) A message comes up, asking if they want to overwrite the old file
4.) 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 there

Is 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.";

I want it to print out like:
Hello, all
my name is Jonathan or 'Jon' for short.


[Edited on April 16, 2008 at 10:02 AM. Reason : .]

4/16/2008 10:01:05 AM

qntmfred
retired
40553 Posts
user info
edit post

$newstring = str_replace(";", "\n", $string);

4/16/2008 10:22:55 AM

vertigo
Veteran
135 Posts
user info
edit post

^ 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);

it gives me a syntax error (for obvious reasons).

Also, some of the fields have newline (line feeds) in there. Can I just use chr(10) and chr(13) as identifiers to make them drop a line?

4/16/2008 10:57:10 AM

DeltaBeta
All American
9417 Posts
user info
edit post

$newstring = stripslashes($string);

4/16/2008 11:04:09 AM

vertigo
Veteran
135 Posts
user info
edit post

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

DeltaBeta
All American
9417 Posts
user info
edit post

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

qntmfred
retired
40553 Posts
user info
edit post

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

DeltaBeta
All American
9417 Posts
user info
edit post

LOL I remember that thread.

What a n00b!

4/16/2008 11:33:13 AM

qntmfred
retired
40553 Posts
user info
edit post

4/16/2008 11:35:51 AM

vertigo
Veteran
135 Posts
user info
edit post

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
I'd like to be able to make it look like:
husbandname & wifename lastname


Does that make sense? The whole if/else setup is clear to me, I just don't know how to break the string into two parts - before the first comma (last name) and after the first comma (first names). Once I can do that, it should be a simple thing to remove the first comma and replace the second comma with an ampersand.

4/16/2008 11:49:23 AM

qntmfred
retired
40553 Posts
user info
edit post

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

BigMan157
no u
103352 Posts
user info
edit post

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

quagmire02
All American
44225 Posts
user info
edit post

what's the advantage of ^^ over ^ and vice versa?

4/16/2008 4:19:14 PM

qntmfred
retired
40553 Posts
user info
edit post

in terms of resources

explode < split < preg_match < preg_match_all

and i think mine is easier to read

4/16/2008 4:28:01 PM

vertigo
Veteran
135 Posts
user info
edit post

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>";
This works fine, except that there are some records that are empty (for example, if you're on record 1000, and you click "next", and record 1001 has nothing, it just shows you a blank page). Since it's a blank page, the "previous" and "next" links aren't there, either. Any suggestions for how I might go about checking the next record to make sure it's valid, and if it isn't, skip to the next one that is? Thanks!

[Edited on April 17, 2008 at 10:01 AM. Reason : CSS]

4/17/2008 9:58:55 AM

DeltaBeta
All American
9417 Posts
user info
edit post

In your select statement, only select rows that have data in them.

4/17/2008 10:57:13 AM

vertigo
Veteran
135 Posts
user info
edit post

^ 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";
because if there is no project name in the database, then it's not a valid record. It doesn't throw me any errors, but when I got to the record before it (let's say record 1000) and click "next", it still shows record 1001 as a blank page. Did I miss something?

4/17/2008 11:47:19 AM

DeltaBeta
All American
9417 Posts
user info
edit post

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

vertigo
Veteran
135 Posts
user info
edit post

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'];
}

?>


Now, I realize that different databases require different syntax for different types of connections. Here is what I know the MySQL connection should look like:

<?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;
}

?>


The problem is that in all of the examples I've found, the $host is "localhost", but in my case, the database is not hosted on my web server - it's somewhere else, the same as it is in the above Oracle example. However, in the Oracle example there's the syntax in the $host definition that refers to the URL and the name of the database. How do I do this in PHP for MySQL?

Thanks for your help!

[Edited on July 3, 2008 at 9:29 PM. Reason : formatting]

7/3/2008 9:26:26 PM

BigMan157
no u
103352 Posts
user info
edit post

http://us3.php.net/manual/en/function.mysql-select-db.php

is 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

Donogh5
All American
971 Posts
user info
edit post

ugh, 4 space tabs ftl

7/4/2008 5:30:54 AM

vertigo
Veteran
135 Posts
user info
edit post

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());
}

?>


Would that work? Also, is there any particular reason to use "die" over "exit"? The PHP manual says that they're equivalent.

Again, thanks for the help!

[Edited on July 6, 2008 at 5:09 PM. Reason : formatting]

7/6/2008 5:08:05 PM

 Message Boards » Tech Talk » PHP - form, search, passing results to new page Page [1] 2, Next  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.38 - our disclaimer.