
andy_b_1502
Members-
Posts
397 -
Joined
-
Last visited
Everything posted by andy_b_1502
-
Hello, i would like to know why this script isnt working the way i want it to: <form method="get" action="localarea.php"> <a href="localarea.php"><?php $_POST[""" .$var. ""</p>"]; echo """ .$var. ""</p>";?></a> </form> Where the [""" .$var. ""</p>"] is the result of a search by the user. I'm trying to take that result from the form and post it on "localarea.php". So far it just displays "Array ( )" where i would like it to display the users search result? any suggestions on this one? If you guys need any more information i will try my best to provide. Thank in advance for any help
-
Taking results from search and echo() ?
andy_b_1502 replied to andy_b_1502's topic in PHP Coding Help
Its wrong i'm not sure what's wrong on it as it works fine for the first part? okay first of all thank you for getting back to me. Secondly, i'm confused on how to write the code, i know what i want it to do but when researching it's not sinking in? Is it possible using my far too long script to get the results of the postcode using query $query = "(SELECT postcode FROM freelistings WHERE postcode like '%$trimmed%') UNION (SELECT postcode FROM basicpackage WHERE postcode like '%$trimmed%') UNION (SELECT postcode FROM premiumuser WHERE postcode like '%$trimmed%') ORDER BY postcode"; Aswell as display the field results of another column next to it - for example: if "postcode" matches show "company_name" next to it Postcode Company Name WS12 2SH - Daves Auto's ive found this so far it pull's the postcode fine but getting the company name to show with the postcode is my problem. Can i have multiple queries using SELECT company_name FROM? or should i use JOIN with just one query? Many thanks for your help so far -
Taking results from search and echo() ?
andy_b_1502 replied to andy_b_1502's topic in PHP Coding Help
Have i described my problem accorindingly? sorry if its a little confusing but on this end my heads imploding.... Any help pointing me in the right direction for this would be appreciated, thanks -
Hi, I have a searchbar which looks for a value in my tables, it works great but i want to know how i can make the results of the search clickable; go to a page relevent to that search, let me explain in more depth. User searches for a postcode, script finds and displays the postcode on the next page, user clicks the result postcode to goto the next page. What im stuck with is how to take that result and automatically display it on the clicked "final" page? Search script: <?php // Get the search variable from URL $var = @$_GET['q'] ; $trimmed = trim($var); //trim whitespace from the stored variable // rows to return $limit=10; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } //connect to your database ** EDIT REQUIRED HERE ** mysql_connect("xxxxx","xxxx","xxxx"); //(host, username, password) //specify database ** EDIT REQUIRED HERE ** mysql_select_db("removalspacelogin") or die("Unable to select database"); //select which database we're using // Build SQL Query $query = "(SELECT postcode FROM freelistings WHERE postcode like '%$trimmed%') UNION (SELECT postcode FROM basicpackage WHERE postcode like '%$trimmed%') UNION (SELECT postcode FROM premiumuser WHERE postcode like '%$trimmed%') ORDER BY postcode"; // EDIT HERE and specify your table and field names for the SQL query $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); $result = mysql_query($query) or die("<b>Query:</b> $query<br><b>Error</b>: " . mysql_error()); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; // google echo "<p><a href=\"http://www.google.com/search?q=" . $trimmed . "\" target=\"_blank\" title=\"Look up " . $trimmed . " on Google\">Click here</a> to try the search on google</p>"; } // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results for $q"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row['postcode']; echo " $title" ; $count++ ; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10</a>  "; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>"; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<p>Showing results $b to $a of $numrows</p>"; ?> <form method="post" action="localarea.php"> <a href="localarea.php"><?php echo """ .$var. ""</p>";?></a> </form> [\code] The form at the bottom im guessing is where i should take the $var from but how exactly? or if not where should it be pulled from to display on the next page? I want this "next" page because ideally there would be lots of results in postcode, so the user can choose which one is more relevant to them and go to that specific page with all the companies within that postcode. Hope this helps for a better understanding of what im trying to achieve, its just implementing it in?
-
Possibly? thanks anyway i think i'll just leave it for now until the future.
-
The field name in my table "users" is called "username" That is the name of the $_session created when they log-in. $_session['username'] If i call session like: echo '<div class="medium">Welcome'; echo ", {$_SESSION['first_name']}!"; Is doesn't give me any header errors, however it doesn't display the users username either. It reads: Welcome, ! Where it should read: Welcome, (username)!
-
Im having a little trouble getting this. Ive put the session_start (); at the top of the html page at line 1 and i get headers already sent error. I also placed this code where i would like the username to be displayed: <?php session_start (); $_SESSION['username'] = $username; echo "Hello ".$_SESSION['username']; ?> But i get the same error message, i wonder whats happening here?
-
Thanks guys! Does that code need to go on line 1, before <!DOCTYPE html PUBLIC.... or where i want the user's username to be displayed?
-
Thanks, i called session start () at line 1, so it puts it before anything to the browser, <?php session_start(); ?> then i take it it should display the username where ever i want it to using: <?php $con = mysql_connect("removalspacecom.ipagemysql.com","removalspace","123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("removalspacelogin", $con); $_SESSION['username'] = $username; echo "Hello ".$_SESSION['username']; mysql_close($con); ?> but im getting header error? what do you think i could to do to stop this error from occuring? specific error code is: Warning: session_start() [function.session-start]: open(/home/users/web/b109/ipg.removalspacecom/cgi-bin/tmp/sess_1719827ba3cdce3c8642d3ff0bd404fc, O_RDWR) failed: No such file or directory (2) in /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/localarea.php on line 2 Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/localarea.php:2) in /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/localarea.php on line 2 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/localarea.php:2) in /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/localarea.php on line 2
-
Okay, is there a way of taking the username from the login script i posted earlier and using that to display it on the next page (with the session script)? Sorry i should have asked that question in the first instance, that is what i need isnt it.
-
Thanks juddster. I wonder if you or anyone could help me with my original problem to displaying the username, currently with the session it displays "Hello:" only so somethings going wrong somewhere it's just knowing where to look?
-
To display the username of the user. the log in script stores the username in a variable i want it to be displayed on the memebers area of the site. should i use session like this: <?php $con = mysql_connect("removalspacecom.ipagemysql.com","removalspace", "123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("removalspacelogin", $con); $_SESSION['username'] = $username; echo "Hello ".$_SESSION['username']; ?> this is the log-in script that takes the $username: <?php /* "Warning: Cannot modify header information - headers already sent by " To avoid the header error , give value zero to $mosConfig_locale_debug = 0; $mosConfig_locale_use_gettext = 0; */ $mosConfig_locale_debug = 0; $mosConfig_locale_use_gettext = 0; ob_start(); if (isset($_POST['submitted']) && !empty($_POST['myusername']) && !empty($_POST['mypassword'])) { //check for submit button click $username = $_POST['myusername']; $password = $_POST['mypassword']; // NOTE - quick check $_SESSION['username'] = $username; echo "Hello ".$_SESSION['username']; // proceed with rest of code $self = $_SERVER['PHP_SELF']; $referer = $_SERVER['HTTP_REFERER']; #if either form field is empty return to the log-in page if( (!$username ) or (!$password ) ){ header( "Location:$referer" ); exit(); } #connect to mysql $conn = @mysql_connect( "server", "username", "password" ) or die( "Could not connect" ); #select database $rs = @mysql_select_db( "database", $conn ) or die ( "Could not select db" ); #create query $sql="select * from users where username = '". $username."' and upassword = '".$password."'"; #execute query $rs = mysql_query( $sql, $conn ) or die( mysql_error() ); if($rs){ $_SESSION['username'] = $username;} #get number of rows that match username and password $num = mysql_numrows( $rs ); #if there is a match the log-in is done if( $num != 0 ){ header( "Location:members_area.php" ); } else { header( "Location:$referer" ); exit(); }} else { ?>
-
Thanks, something on the lines of this: $result = mysql_query("SELECT username FROM users WHERE username='$username'"); while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['username'] . "</td>";; echo "</tr>";} echo "</table>"; ?> i would like it to take their username value from when they click log-in and it display in the memebr's area?
-
Could somebody help to pint me in the right direction where to look for the correct syntax for my problem. The user logs in and is sent to the members area, their username is echoed out to display that they are logged in, my problem is that it is displaying all the users in my database (currently 2 usernames). I would just like to have the current users username displayed. <?php $con = mysql_connect("server","username", "password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $result = mysql_query("SELECT username FROM users" ); while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['username'] . "</td>";; echo "</tr>";} echo "</table>"; ?> What sholud i research to perform this task properly? thank you.
-
Sorry no, i didnt put the error reporting in. Once i did that it worked, could you please explain why putting in error reporting made it work correctly, i dont understand why that would help? Thank you very much for your help guys!
-
I just tried to change the field name "postcode" on each table and tried the new code: $query = "(SELECT fpostcode FROM freelistings WHERE fpostcode like '%$trimmed%') UNION (SELECT bpostcode FROM basicpackage WHERE bpostcode like '%$trimmed%') UNION (SELECT ppostcode FROM premiumuser WHERE ppostcode like '%$trimmed%') ORDER BY postcode"; but unfortunatley i still get error message: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/search.php on line 44 Results Sorry, your search: "ws122sh" returned zero results Click here to try the search on google Couldn't execute query
-
Sorry for posting the whole code and thank you very much. From the help you've given me so far i've looked up on the WHERE clause. So the WHERE is used to exctract only those records, in this case "postcode" like the searched postcode for the user. Therefore the WHERE clause should be under each SELECT like this so its matching the search with the MySQL fields in the tables: $query = "select postcode from freelistings WHERE postcode like \"%$trimmed%\" order by postcode UNION select postcode from basicpackage WHERE postcode like \"%$trimmed%\" order by postcode UNION select postcode from premiumuser WHERE postcode like \"%$trimmed%\" order by postcode"; I know that the UNION is combining them together, the WHERE is taking only the postcode value and the ORDER BY sorts the data. This script is failing so ive not sussed it yet, ive turned on error repporting and placed it at the top of the script with: ini_set(‘display_errors’,1); but all i get is: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/search.php on line 47 Results Sorry, your search: "ws122sh" returned zero results Click here to try the search on google Couldn't execute query error_reporting(E_ALL|E_STRICT); that postcode is definatley in one of the tables but it's not picking it up?
-
Thanks: <?php // Get the search variable from URL $var = @$_GET['q'] ; $trimmed = trim($var); //trim whitespace from the stored variable // rows to return $limit=10; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } //connect to your database ** EDIT REQUIRED HERE ** mysql_connect("server","username","password"); //(host, username, password) //specify database ** EDIT REQUIRED HERE ** mysql_select_db("removalspacelogin") or die("Unable to select database"); //select which database we're using // Build SQL Query $query = "select postcode from freelistings UNION select postcode from basicpackage UNION select postcode from premiumuser where postcode like \"%$trimmed%\" order by postcode"; // EDIT HERE and specify your table and field names for the SQL query $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; // google echo "<p><a href=\"http://www.google.com/search?q=" . $trimmed . "\" target=\"_blank\" title=\"Look up " . $trimmed . " on Google\">Click here</a> to try the search on google</p>"; } // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results for $q"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row['postcode']; echo " $title" ; $count++ ; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10</a>  "; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>"; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<p>Showing results $b to $a of $numrows</p>"; ?> <form method="post" action="localarea.php"> Click here to go to your local area <input type="submit" name="Submit" value="go" /> </form>
-
Okay well here's the problem: Used UNION as suggested. I was just testing it out so i inserted two example postcodes in two seperate tables in my database. "WS12 2SH", "WS12 5SH". I searched a completly random letter "q" in my searchbar. The results were: "You searched for: "Q" Results for Q WS12 5SH WS122SH Showing results 1 to 2 of 2 Click here to go to your local area" As you can see, its picking up whatever is in the table fields, regardless of whats being searched? firstly why is this happeneing, secondly what do you need to know to help me remedy this. please note* i will learn by mistakes, a proccess many learn by, i could just trial-and-error for weeks maybe months like how i've done before but why? when there are these forums with more knowledgable folk that are considered experts on php that are willing to share their craft. Just my opinion.
-
Sorry i thought it was a coding help forum, i'll do what you say. Thanks for your help so far.
-
Thank you, i do have a field called postcode in all of my tables although they are not all on the same number order, does this matter? they all match otherwise. What should i be using instead of SELECT * And how do i type UNION to make it work $query = "select * from freelistings UNION basicpackage UNION premiumuser where postcode like \"%$trimmed%\" order by postcode";
-
Just to add, when i alter the code to look like this: $query = "select * from freelistings where postcode like \"%$trimmed%\" order by postcode"; i get a result in my browser: You searched for: "ws122sh" Results1.) Showing results 1 to 1 of 1 which is great, but that's only searching the one table and not the other two? when i change the code to include the others like this: $query = "select * from freelistings,basicpackage,premiumuser where postcode like \"%$trimmed%\" order by postcode"; i get this error message: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /hermes/bosweb25a/b109/ipg.removalspacecom/new - - removalspace.com/search.php on line 35 Results Sorry, your search: "ws122sh" returned zero results Click here to try the search on google Couldn't execute query which is not picking up the example postcode at all?
-
I would like to edit this script's SQL query to SELECT FROM multiple tables within my database. Can this be done first of all and what would i need to change? here's the script: <?php // Get the search variable from URL $var = @$_GET['q'] ; $trimmed = trim($var); //trim whitespace from the stored variable // rows to return $limit=10; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } //connect to your database ** EDIT REQUIRED HERE ** mysql_connect("host","username","password"); //(host, username, password) //specify database ** EDIT REQUIRED HERE ** mysql_select_db("database name") or die("Unable to select database"); //select which database we're using // Build SQL Query $query = "select * from the_table where 1st_field like \"%$trimmed%\" order by 1st_field"; // EDIT HERE and specify your table and field names for the SQL query $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>"; // google echo "<p><a href=\"http://www.google.com/search?q=" . $trimmed . "\" target=\"_blank\" title=\"Look up " . $trimmed . " on Google\">Click here</a> to try the search on google</p>"; } // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; $count = 1 + $s ; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row["1st_field"]; echo "$count.) $title" ; $count++ ; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< Prev 10</a>  "; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>"; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<p>Showing results $b to $a of $numrows</p>"; ?> All i want it to do is search a field in 3 different tables in my database called "postcode" and return search results. Ive seen the JOIN feature but don't know how to use it, any help i would appreciate. Thank you.
-
This is the problem, i had a blank page working before using a script i pulled from another site, that worked but it looked like it didnt belong to my website. I need the code you suggested to work without the user being taken to a blank page and just use my existing form on "login.php" What do i need to take away from your script for it to used with an existing form?