
PHPBear
Members-
Posts
38 -
Joined
-
Last visited
Everything posted by PHPBear
-
What's wrong with my DB query (COUNT, UNION ALL, PDO)
PHPBear replied to PHPBear's topic in PHP Coding Help
Now I get it; I changed $sql = "SELECT SUM(num) to $sql = "SELECT SUM(num) as num and it works great. Thanks! -
What's wrong with my DB query (COUNT, UNION ALL, PDO)
PHPBear replied to PHPBear's topic in PHP Coding Help
Thanks for the tips. I changed that last MyURl to MyURL and have error reporting on now. But I still get the same result - even when I strip the subquery down to just the original table, like this... $sql = "SELECT SUM(num) FROM ( SELECT COUNT(URL) AS num FROM people WHERE URL = :MyURL ) AS X"; -
What's wrong with my DB query (COUNT, UNION ALL, PDO)
PHPBear replied to PHPBear's topic in PHP Coding Help
That's weird; the end of my post doesn't display. I just wanted to add that the value for $Total['num'] - where the URL is MySite/Carl_Sagan - should be 1, but it's 0. When I revert to the original query (just one table), it works. I can't find anything wrong, but I'm not really familiar with subqueries, and I'm still fairly new to PDO. Thanks for any tips. -
Someone gave me a script that works - but it doesn't work for me. I've been trying to debug it, but I'm striking out. First, let's start with a simple query: $sql= "SELECT COUNT(URL) AS num FROM people WHERE URL = :MyURL"; It works, where a URL is MySite/Carl_Sagan and $MyURL = 'Carl_Sagan' I have several similar queries for various websites and sections. I want to try to merge everything into a new website that will function kind of like a mini encyclopedia. So I use UNION ALL to link several queries together, like this... $sql = "SELECT SUM(num) FROM ( SELECT COUNT(URL) AS num FROM pox_topics WHERE URL = :MyURL UNION ALL SELECT COUNT(URL) AS num FROM people WHERE URL = :MyURL UNION ALL SELECT COUNT(Taxon) AS num FROM gz_life WHERE Taxon = :MyURL ) AS X"; $stmt = $pdo->prepare($sql); $stmt->bindParam(':MyUrl',$MyURL,PDO::PARAM_STR); $stmt->execute(); $Total = $stmt->fetch(); switch($Total['num']) { case 1: // ETC.
-
Need help with bread crumbs nav links array/path
PHPBear replied to PHPBear's topic in PHP Coding Help
I just discovered that the links are properly hidden in the code if I remove the htmlentities thing... $link = '<span class="navHere"><b>' . $Path2[$i] . '</b></span>'; However, in the string Topics > Washington > Governor, Topics and Washington are both linked to /Topics. -
Need help with bread crumbs nav links array/path
PHPBear replied to PHPBear's topic in PHP Coding Help
Thanks, Hansford. I think that's the ticket, though I'm doing something wrong. I modified your code a bit to match my query and came up with this: function get_path($dbh,$node,$TopnavTable, $TopnavName) { $stmt = $dbh->prepare("SELECT name FROM $TopnavTable WHERE $TopnavName = ?"); $stmt->bindValue(1,$node); $stmt->execute(); $stmt->setFetchMode(PDO::FETCH_ASSOC); $row = $stmt->fetch(); $path = array_merge(get_path($pdo,$row['Parent'], $TopnavTable, $TopnavName), $path); return $path ; } $Path2 = explode("/", $path); $size = count($Path2); for($i = 0; $i < $size; $i++) { if($i == $size - 1) { $link = htmlentities('<span class="navHere"><b>' . $Path2[$i] . '</b></span>'); } else { $link = htmlentities('<a href="/Topics">' . $Path2[$i] . '</a>'); } $Path2[$i] = $link; } echo join($Path2, ' > '); It works fine except that the links display as plain text. Can you see what I did wrong? -
0down votefavorite I just converted a query that displays "bread crumbs" style navigation links to PDO: function get_path($dbh,$node,$TopnavTable, $TopnavName) { $stmt = $dbh->prepare("SELECT name FROM $TopnavTable WHERE $TopnavName = ?"); $stmt->bindValue(1,$node); $stmt->execute(); $stmt->setFetchMode(PDO::FETCH_ASSOC); $row = $stmt->fetch(); $path = array_merge(get_path($pdo,$row['Parent'], $TopnavTable, $TopnavName), $path); return $path ; } I also figured out a way to display the results: $Path2 = explode("/", $path); $Path2[1] = '<a href="/Topics">'.$Path2[1].'</a>'; $Path2[2] = '<a href="/Topics/'.$Path2[2].'">'.$Path2[2].'</a>'; $Path2[3] = '<span class="navHere"><b>'.$Path2[3].'</b></span>'; echo join( $Path2, ' > ' ); However, it only works only if I'm working with an array consisting of three segments. For example, I'm viewing the URL MySite/Topics/Washington/Governor, which displays the following bread crumbs trail: Topics > Washington > Governor If I view MySite/Washington, it should display... Topics > Washington But I get an error message: Undefined offset: 3, plus there's a trailing arrow (>) after Washington. So I'm trying to figure out how to make this work with any number of segments - 2, 3, 6, etc. Regardless of the number of segments, I'd like the last segment to be unlinked. (I'm going to further put it inside a span.) Does anyone have any tips? (If there's a completely different way of doing it that's better, I'd love to hear about it.)
-
I've just started learning about arrays and am still taking baby steps. I'd like to ask how to place values from a database table in various groups. From the beginning, I have a database-driven website with pages for thousands of animal species (table gz_life). I have a second table that lists species that are somehow unique, including species that are extinct. In fact, there are three categories of extinction, as illustrated below... Field: TaxonG | Wild_Card Dinosaurs | Prehistoric Trilobites | Prehistoric Mammoth | Ice_Age Mastodon | Ice_Age Passenger_Pigeon | Extinct Dodo | Extinct I would like to somehow classify the above animals into groups, so I can more easily manipulate information relating to them. For example, imagine if I wanted to link pages of living animals to files in one directory and pages representing extinct creatures to a different directory. I might create a switch similar to this: switch($Group) { case 'Extinct': case 'Ice_Age': case 'Prehistoric': (Link to Folders/Extinct/MyPage.php break; default: (Link to Folders/Living/MyPage.php break; ) So how can I put all these URL's into groups or arrays so I can manage them? Below is the basic script I'm working with... <?php $result = mysql_query('select count(*) from gz_ghosts'); if (($result) && (mysql_result ($result , 0) > 0)) { } else { die('Invalid query: ' . mysql_error()); } { $Children = mysql_query ("SELECT N, TaxonG, Wild_Card FROM gz_ghosts"); } while ($row = mysql_fetch_array($Children, MYSQL_ASSOC)) { $TaxonG = $row['TaxonG']; $Wild_Card = $row['Wild_Card']; ?> If I add WHERE Wild_Card = 'Ice_Age' to the query, then I can create the following array: $Icy = array($TaxonG); It seems to work, but I can't do much with it, because I don't know how to manipulate it or associate it with URL's. I also discovered the foreach function. If I replace $v with $MyURL, it works - but only on the page MySite/Life/Mammoth. I don't know how to replace $v with an array value that will work for every page matching the array's contents. foreach ($Icy as $k => $v) { if ($v == "Mammoth") { echo "Hello World"; } } I've also played with the in_array function, with a little success, but it still isn't working correctly. I've tried combining foreach and in_array and have tried my experiments both inside and outside the loop. * * * * * Anyway, getting back to the first query I posted (without a where clause), how can I put these values into separate groups and manipulate them? I thought I could create a switch similar to the one below, but I don't know exactly how to construct it. switch($Wild_Card) { case 'Parataxon': $Group = 'Parataxon'; // OR... $Parataxon = 'array($Wild_Card)'; break; case 'EZ': $Group = 'EZ': // OR... $EZ = 'array($Wild_Card)'; break; case 'Extinct': $Extinct = 'array($Wild_Card)'; break; case 'Ice_Age': $Ice_Age = 'array($Wild_Card)'; break; case 'Prehistoric': $Prehistoric = 'array($Wild_Card)'; break; case 'Ghost': $Ghost = 'array($Wild_Card)'; break; default: $Joker = ''; break; } [/code] Anyway, I think that pretty well explains what I'm trying to do. Any tips? Thanks! P.S. I've looked at the PHP Manual - http://php.net/manua...res.foreach.php - but it doesn't make a lot of sense to me.
-
Yikes - I must have been doing something wrong; in fact, your script works fine. Thanks!
-
Hmmm, now I get this error message... Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /Users/myname/Sites/... If I change mysql_fetch_assoc() to mysql_fetch_result(), I get this error message... Fatal error: Call to undefined function mysql_fetch_result() I Googled some of those error messages a little more, and I found one discussion where they concluded that the individual had a problem with his PHP or MySQL installation; they wanted him to modify his php.ini file. Do you think that might be my problem? I'd like to verify it before I start searching for hidden files. I did think it was kind of strange that database queries that worked fine suddenly stopped working after I upgraded MAMP. I though I just had to update my code somehow. Thanks.
-
The current incarnation (the one I posted) eliminated the error messages. However, if the URL is MySite/World/Spain, $result should = 1. Instead, it echoes Resource id #13.
-
I just did a massive hardware/software upgrade - first new computer (a Mac) in 6 1/2 years, first MAMP upgrade in several years, etc. I'm now using these software versions: Apache 2.2.21, PHP 5.3.6, MySQL 5.5.9 Just about everything seems to be working fine except my database connections. I appear to be making a simple mistake, but I can't figure out the solution. I've checked out all sorts of online help pages, but I'm only getting more confused. Rather than ask what the problem is, it might be better to go back to square one and ask how you would write this query from scratch. I pasted my current, somewhat convoluted code below, to give you an idea of how I was doing it. But here's what I'm trying to do: Imagine a website with a static page at World: MySite/World. It displays dynamic pages, like these: MySite/World/France, MySite/World/Spain...just as long as "France" or "Spain" match values stored in a database table (gw_geog). In my query, I represent France, Spain and other place names with the variable $MyURL. So I want to query that database table. If $MyURL = 'Japan' (e.g. MySite/World/Japan), then $result should = 1, fetching a web page. If $result = 0 (e.g. a misspelled URL, like MySite/World/Japax), then it fetches a 404 error page. If $result = 2 or more (e.g. MySite/World/Georgia - the name of a country and a U.S. state), then it fetches a duplicate note. I wrote this script years ago, when I didn't know much about PHP/MySQL (I still don't), and I've upgraded my software, so I need to start fresh. Can anyone tell me how YOU would a script to accomplish this task? Thanks. * * * * * <?php // DATABASE CONNECTION $conn = mysql_connect("localhost", "Username", "Password"); if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db("db_general")) { echo "Unable to select db_general: " . mysql_error(); exit; } // DATABASE QUERY $sql = " SELECT COUNT(URL) FROM gw_geog WHERE URL = '$MyURL' "; $sql_result = mysql_query($sql,$conn); $result = $sql_result; // DO SOMETHING WITH THE RESULTS switch ($result) { case 1: echo "\n"; include_once($BaseINC."/$MyPHP/inc/B/DB2/Child/World.php"); include_once($BaseINC."/$MyPHP/inc/B/Child2.php"); include_once($BaseINC."/$MyPHP/inc/D/Child.php"); echo "\n"; break; case 0: include_once($BaseINC."/404.php"); break; default: // More than one results, as in Georgia (state and republic) include_once($_SERVER['DOCUMENT_ROOT']."/Dupe.php"); echo "\n"; break; } ?>
-
I have a series of websites with World sections that display pages at URL's like... www.MySite.com/World www.MySite.com/World/Mexico www.MySite.com/World/Hawaii ...where Mexico and Hawaii are values stored in a database table. However, one site is different in that the site's home page is also the home page for the World section (and other sections). It displays URL's like this: www.MySite.com (equivalent to www.MySite.com/World above) www.MySite.com/Mexico www.MySite.com/Hawaii There are some conflicts between my home page and section home pages (e.g. MySite/Topics/index.htm) in my .htaccess file. If I make it work online, then it doesn't work on my local sites. So I want to try making it work with just ONE static page on the entire site: MySite.com/index.php. Below is an example of the code I use to allow pages from the World section to display: $result = mysql_result(mysql_query("SELECT COUNT(URL) FROM gw_geog_urls WHERE URL = '$MyURL2'"),0); switch ($result) { case 1: break; case 0: include_once(404.php"); break; default: include_once(Dupe.php"); break; " I'd like to modify my script so that pages from other sections will also display. Let's use the Topics section as an example. I'd like to find a script using PHP and/or REGEX that says... if the $result is 0, display a 404 error page, UNLESS the domain name is followed by the word Topics. ANY of the following would qualify: MySite/Topics MySite/Topics/ MySite/Topics/Food MySite/Topics/Food/Grains MySite/Topics/xYz If the URL is any of the above, that will trigger an include that will handle the Topics section, which will allow the first four URL's and display a 404 error page in place of MySite/Topics/xYZ. This would NOT qualify, because the word Topics doesn't follow the domain name: MySite/Arizona/Topics It might still be displayed, but it's already taken care of with some additional code I've written. I would also like to assign the value $MySection = Topics to any and every URL in which Topics follows the domain name. Using another example, let's consider the Reference section. MySite/Reference MySite/Reference/ MySite/Reference/Horses MySite/Reference/Typo MySite/References/ The first four pages should have the value $MySection = Reference. The fifth one doesn't match, because it says ReferenceS (with an s at the end). The first four URL's will fetch an include which will weed out the fourth URL (404 error page) but display regular pages for the first three. Sorry for the long explanation, but I wanted to make sure I explained it clearly. Thanks for any tips!