I'm a relative newbie trying to make my first database-driven website. As a result of all my experiments, I now have three separate queries, as follows:
$query = " SELECT IDArea, Name, NNGW, QGW, SigGW
FROM basics
WHERE IDArea LIKE '$mycode'";
$data = array();
$res = mysql_query($query);
if ($res == mysql_query($query)) { while ($temp = mysql_fetch_assoc($res)) { $data[] = $temp;} }
if (mysql_errno()){ echo "MySQL error ".mysql_errno().": ".mysql_error()."\n<br />When executing:<br />\n$query\n<br />";
exit; }
while ($temp = mysql_fetch_assoc($res)){ $data[] = $temp; }
$qry = "SELECT a.Name, a.AreaType FROM basics as b, area as a WHERE a.IDArea = b.IDArea and b.IDArea = '$mycode'";
$result = mysql_query($qry);
$type = mysql_fetch_array($result);
$mytype = "my" . $type['AreaType'];
$$mytype = $type['Name'];
$query = " SELECT IDArea, Parent1, Parent2
FROM parent
WHERE IDArea LIKE '$mycode'";
$data = array();
$res = mysql_query($query);
if (mysql_errno()){ echo "MySQL error ".mysql_errno().": ".mysql_error()."\n<br />When executing:<br />\n$query\n<br />";
exit; }
while ($temp = mysql_fetch_assoc($res)){ $data[] = $temp; }
I couldn't delete any single query without screwing something up. In particular, the last query - connecting with the table named "parents" - knocks some functions out of commision, yet if I remove it, other items don't work.
So I tried to figure out how to combine the queries into a single query, and I wound up with this...
SELECT area.*, basics.*, parent.* FROM area, basics, parent WHERE
(
(parent.IDArea = area.IDArea) AND
(area.IDArea = basics.IDArea) AND
(basics.IDArea = '$mycode')
)
but I got a parse error, so I changed it to this...
$query = "SELECT area.*, basics.*, parent.* FROM area, basics, parent WHERE
(
(parent.IDArea = area.IDArea) AND
(area.IDArea = basics.IDArea) AND
(basics.IDArea = '$mycode')
)";
Now I get lots of errors similar to these:
Warning: Invalid argument supplied for foreach()
Warning: reset(): Passed variable is not an array or object in C:\sites\edrevolt\world\eur\deu\index.php on line 75
The obvious reason is that my new query omits some of the special functions that were built into the queries it replaced. So I'd like to know how to add them to my new query.
To put everything into perspective, let me offer a brief overview of what I'm trying to accomplish.
Using includes, database variables and echo functions, every page on my site will be absolutely identical except for this:
$mycode = '[something or other]';
For example, one page might be
$mycode = 'usa'; and another
$mycode = 'mex';.
These two pages represent the United States and Mexico. "usa" and "mex" are values that are also found in a field named IDArea on several tables, including "area," "parent" and "basics."
If a page is identified as "mex" (Mexico), the name "Mexico" will be drawn from the table "area," the Capital (Mexico City) from the table "basics" and Mexico's "parent" (North America) from the table "area."
I've been working on this for weeks, studying tutorials and asking questions on forums. I've been advised to use many different strategies, none of which I fully understand. However, I got it to the point where virtually everything was working.
I THINK all I have to do is refine my query, before proceeding to the final obstacle - incorporating a "parent-sibling" relationship into the whole thing.
I also posted my source code for the entire page below.
I suspect most pros will find the solution much simpler than my explanation. 🙂
<?php error_reporting(E_ALL);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<?php
$mycode = 'deu';
[DATABASE CONNECTION]
// DELETED QUERIES...
/*
$query = " SELECT IDArea, Name, NNGW, QGW, SigGW
FROM basics
WHERE IDArea LIKE '$mycode'";
$data = array();
$res = mysql_query($query);
if ($res == mysql_query($query)) { while ($temp = mysql_fetch_assoc($res)) { $data[] = $temp;} }
if (mysql_errno()){ echo "MySQL error ".mysql_errno().": ".mysql_error()."\n<br />When executing:<br />\n$query\n<br />";
exit; }
while ($temp = mysql_fetch_assoc($res)){ $data[] = $temp; }
$qry = "SELECT a.Name, a.AreaType FROM basics as b, area as a WHERE a.IDArea = b.IDArea and b.IDArea = '$mycode'";
$result = mysql_query($qry);
$type = mysql_fetch_array($result);
$mytype = "my" . $type['AreaType'];
$$mytype = $type['Name'];
$query = " SELECT IDArea, Parent1, Parent2
FROM parent
WHERE IDArea LIKE '$mycode'";
$data = array();
$res = mysql_query($query);
if (mysql_errno()){ echo "MySQL error ".mysql_errno().": ".mysql_error()."\n<br />When executing:<br />\n$query\n<br />";
exit; }
while ($temp = mysql_fetch_assoc($res)){ $data[] = $temp; }
*/
$query = "SELECT area.*, basics.*, parent.* FROM area, basics, parent WHERE
(
(parent.IDArea = area.IDArea) AND
(area.IDArea = basics.IDArea) AND
(basics.IDArea = '$mycode')
)";
$myname = ''; foreach ($data as $r){ $myname .= $r['IDArea']; }; // 'Name' didn't work.
$mykon = ''; foreach ($data as $r){ $mykon .= $r['IDArea']; }; // 'Name' didn't work.
$kon = ''; foreach ($data as $r){ $kon .= $r['Parent1']; }; // Renamed body from eur to ceu to make this work.
$mynat = ''; foreach ($data as $r){ $mynat .= $r['Parent1']; };
$nat = ''; foreach ($data as $r){ $nat .= $r['Parent1']; };
$mysta = ''; foreach ($data as $r){ $mysta .= $r['Parent1']; };
$sta = ''; foreach ($data as $r){ $myname .= $r['Parent1']; };
$nickname = ''; foreach ($data as $r){ $nickname .= $r['IDArea']; }; // 'NNGW' didn't work.
<head>
<?PHP
include ($_SERVER['DOCUMENT_ROOT']."/a1/inc/head/world.php");
include ($_SERVER['DOCUMENT_ROOT']."/a1/inc/head/$kon.php");
include ($_SERVER['DOCUMENT_ROOT']."/a1/inc/head.php");
</head>
<h1 class="title" id="title<?=$mycode"><?php echo "$myname" </h1>
<div class="divtop">
<br />
<div class="subtitle" id="sub<?=$mycode"><span class="subspan">
<?php foreach ($data as $r){ print $r['NNGW']; }
</span></div>
<div class="quotewrap" id="qwrap<?=$mycode">
<div class="quote" id="q<?=$mycode">
<?php reset($data); while ($r = each($data)){ print $r[1]['QGW']; } </div>
<div class="sig" id="sig<?=$mycode">
<?php reset($data); while ($r = each($data)){ print $r[1]['SigGW']; } </div>
</div><!-- EndQuoteWrap -->
<div class="toplinks" id="toplinks<?=$mycode"><a href="/index.php">EdRevolt Home</a> >
<?=$sectionlink<?php echo "$continentlink" <?php echo "$nationlink"
<?php echo "$statelink" <?php echo "$myname" </div>
</div><!-- EndDivTop -->
<body class="<?php echo "$type" " id="<?php echo "$mycode" " onload="P7_swapClass(0,'none','showIt','hideIt','div');typeWriter('quote',100)">
<?php include ($_SERVER['DOCUMENT_ROOT']."/a1/inc/body.php");
</body>
</html>