PHP site problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

mikeyboy
Forum Newbie
Posts: 13
Joined: Sat Jun 07, 2008 8:19 am

Re: PHP site problems

Post by mikeyboy »

Many many thanks for your response.

Here is the code now with your suggestions included.

Code: Select all

 
<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
 
//$CategoryID=$_GET["CategoryID"];
//$CategoryName=$_GET["CategoryName"];
$GroupID=$_GET["GroupID"];
$Group=$_GET["Group"];
$ManufacturerID=$_GET["ManufacturerID"];
$ManufacturerName=$_GET["ManufacturerName"];
//$ManufacturerCategoryID=$_GET["ManufacturerCategoryID"];
 
echo "Microsoft.Jet.OLEDB.4.0";
$conn=mysql_connect("localhost","XXXX","XXXX");
mysql_select_db("XXXX",$conn);
 
$ManufacturerID = ("ManufacturerID");
$ManufacturerCategoryID = ("ManufacturerCategoryID");
$ManufacturerName = ("ManufacturerName");
$CategoryID = ("CategoryID");
$CategoryName = ("CategoryName");
 
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0" cool gridx="8" gridy="8" showgridx showgridy usegridx usegridy><tr height="8"><td align="center"><img src="images/CGLLogoSmall.jpg"></td></tr></table>
 
<hr />
<br />
<table width="100%">
<tr><td align="center" class="heading"><a href="Gallery.php" class="heading">Gallery</a> - <a href="Group.php?Group=<? print $Group;?>&GroupID=<? print $GroupID;?>" class="heading"><? print $Group;?> Prints</a> - <? print $ManufacturerName;?></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td colspan="4" align="center"><strong>Please select a Category</strong></td></tr>
<tr><td colspan="4" align="center">
 
<table width="90%"><tr><td valign="top"></td><td><table class="heading" width="500px"><td align="left">
 <?php
 $rs=mysql_query("SELECT Category.CategoryID AS CategoryID, Category.CategoryName, Manufacturer.ManufacturerID AS ManufacturerID, Manufacturer.ManufacturerName, ManufacturerCategory.ManufacturerCategoryID, ManufacturerCategory.ManufacturerID, ManufacturerCategory.CategoryID FROM Category, Manufacturer, ManufacturerCategory WHERE Category.CategoryID = ManufacturerCategory.CategoryID AND Manufacturer.ManufacturerID = ManufacturerCategory.ManufacturerID AND Manufacturer.ManufacturerID = '" & ManufacturerID & "' Order by Category.CategoryName");
 if ($rs)
 { //This will check if the query failed or not
   if (mysql_num_rows($rs) > 0)
   {  //This will check if there were any results returned from the query
     while ($row = mysql_fetch_array($rs))
     {
       print "<form name=\"Paintingsform\" method=\"post\" action=\"Paintings.php\">";
       print "<input type=\"hidden\" name=\"CategoryID\" value=". $row['CategoryID'] ." />";
       print "<tr><td align=\"left\">";
       print "<a href=\"Paintings.php?CategoryID=". $row['CategoryID'] ."&CategoryName=". $row['CategoryName'] ."&Group=". $row['Group'] ."&GroupID=". $row['GroupID']  ."&ManufacturerID=". $row['ManufacturerID'] ."&ManufacturerName=". $row['ManufacturerName'] ."&ManufacturerCategoryID=". $row['ManufacturerCategoryID'] ."\" class=\"heading\">";
       print "</td><td align=\"left\">";
       print "<a href=\"Paintings.php?CategoryID=". $row['CategoryID'] ."&CategoryName=". $row['CategoryName'] ."&Group=". $row['Group'] ."&GroupID=". $row['GroupID']  ."&ManufacturerID=". $row['ManufacturerID'] ."&ManufacturerName=". $row['ManufacturerName'] ."&ManufacturerCategoryID=". $row['ManufacturerCategoryID'] ." \" class=\"heading\">". $row['CategoryName'] ."</a>";
       print " - <span class=\"subheading\"></span></td><td></tr></form>";
     }
   }
   else
   {
     print "no results found"; //Edit this line as needed for what you need displayed when no database records are found in the query
   }
 }
   else
 {
   print "Not Available";
 }
 
mysql_close($conn);
 ?>
 
The error i get now is to do with the SELECT statement:
Notice: Use of undefined constant ManufacturerID - assumed 'ManufacturerID'
Under that is shows as Not Available (so i'm guessing its bypassing the form again)

As far as i can tell ManufacturerID is being pulled from the previous page. It shows up fine in the link in the url and subsequently the GET at the top doesn't throw up any errors. I tried putting $ManufacturerID = ("ManufacturerID"); in under the connection statements too, but that has no impact.
Towards the end of the select statement, i changed the ManufacturerID to just have ' either side and also with ` but that simply removed the error and went straight to the unavailable print again :(

Thanks for your previous assistance. Might i impose on your continued expert help, or that of any other board members.

Mikey
mikeyboy
Forum Newbie
Posts: 13
Joined: Sat Jun 07, 2008 8:19 am

Re: PHP site problems

Post by mikeyboy »

Spent most of my weekend trying to sort this code and even away from the PC its been going round in my head. Last night i nearly drove my gf nuts talking in code ;)

:banghead:
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: PHP site problems

Post by dbemowsk »

In your query you have

Code: Select all

'" & ManufacturerID & "'
change this to

Code: Select all

'".$ManufacturerID ."'
The reason you are seeing the "Not Available" is that the query is failing at "if ($rs)" meaning $rs is evaluating to false because your query is incorrect as indicated above.
mikeyboy
Forum Newbie
Posts: 13
Joined: Sat Jun 07, 2008 8:19 am

Re: PHP site problems

Post by mikeyboy »

Ok now its not giving me a code error, but its still going straight through to the "no results found".

As its pulling the GET data through from the previous page, there should be 2 results from the query. It should be showing the 2 painting types that match the manufacturer.

Earlier you mentioned how to use the do statement correctly, but then you suggested modifications to my code without those possibilities for the do. Did the code you suggested replace it or should it still be in there somewhere?

Thanks for your continued help

Mikey

Code: Select 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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="test.css" />
 
</head>
 
<body>
<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
 
//$CategoryID=$_GET["CategoryID"];
//$CategoryName=$_GET["CategoryName"];
$GroupID=$_GET["GroupID"];
$Group=$_GET["Group"];
$ManufacturerID=$_GET["ManufacturerID"];
$ManufacturerName=$_GET["ManufacturerName"];
//$ManufacturerCategoryID=$_GET["ManufacturerCategoryID"];
 
echo "Microsoft.Jet.OLEDB.4.0";
$conn=mysql_connect("localhost","XXXX","XXXX");
mysql_select_db("XXXX",$conn);
 
$ManufacturerID = ("ManufacturerID");
$ManufacturerCategoryID = ("ManufacturerCategoryID");
$ManufacturerName = ("ManufacturerName");
$CategoryID = ("CategoryID");
$CategoryName = ("CategoryName");
 
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0" cool gridx="8" gridy="8" showgridx showgridy usegridx usegridy><tr height="8"><td align="center"><img src="images/CGLLogoSmall.jpg"></td></tr></table>
 
<hr />
<br />
<table width="100%">
<tr><td align="center" class="heading"><a href="Gallery.php" class="heading">Gallery</a> - <a href="Group.php?Group=<? print $Group;?>&GroupID=<? print $GroupID;?>" class="heading"><? print $Group;?> Prints</a> - <? print $ManufacturerName;?></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td colspan="4" align="center"><strong>Please select a Category</strong></td></tr>
<tr><td colspan="4" align="center">
 
<table width="90%"><tr><td valign="top"></td><td><table class="heading" width="500px"><td align="left">
 <?php
 $rs=mysql_query("SELECT Category.CategoryID AS CategoryID, Category.CategoryName, Manufacturer.ManufacturerID AS ManufacturerID, Manufacturer.ManufacturerName, ManufacturerCategory.ManufacturerCategoryID, ManufacturerCategory.ManufacturerID, ManufacturerCategory.CategoryID FROM Category, Manufacturer, ManufacturerCategory WHERE Category.CategoryID = ManufacturerCategory.CategoryID AND Manufacturer.ManufacturerID = ManufacturerCategory.ManufacturerID AND Manufacturer.ManufacturerID = '".$ManufacturerID."' Order by Category.CategoryName");
 if ($rs)
 { //This will check if the query failed or not
   if (mysql_num_rows($rs) > 0)
   {  //This will check if there were any results returned from the query
     while ($row = mysql_fetch_array($rs))
     {
       print "<form name=\"Paintingsform\" method=\"post\" action=\"Paintings.php\">";
       print "<input type=\"hidden\" name=\"CategoryID\" value=". $row['CategoryID'] ." />";
       print "<tr><td align=\"left\">";
       print "<a href=\"Paintings.php?CategoryID=". $row['CategoryID'] ."&CategoryName=". $row['CategoryName'] ."&Group=". $row['Group'] ."&GroupID=". $row['GroupID']  ."&ManufacturerID=". $row['ManufacturerID'] ."&ManufacturerName=". $row['ManufacturerName'] ."&ManufacturerCategoryID=". $row['ManufacturerCategoryID'] ."\" class=\"heading\">";
       print "</td><td align=\"left\">";
       print "<a href=\"Paintings.php?CategoryID=". $row['CategoryID'] ."&CategoryName=". $row['CategoryName'] ."&Group=". $row['Group'] ."&GroupID=". $row['GroupID']  ."&ManufacturerID=". $row['ManufacturerID'] ."&ManufacturerName=". $row['ManufacturerName'] ."&ManufacturerCategoryID=". $row['ManufacturerCategoryID'] ." \" class=\"heading\">". $row['CategoryName'] ."</a>";
       print " - <span class=\"subheading\"></span></td><td></tr></form>";
     }
   }
   else
   {
     print "no results found"; //Edit this line as needed for what you need displayed when no database records are found in the query
   }
 }
   else
 {
   print "Not Available";
 }
 
mysql_close($conn);
 ?>
</table></td></tr></table>
</td></tr>
</table>
<hr />
</body>
</html>
 
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: PHP site problems

Post by dbemowsk »

you may want to do something like this

Code: Select all

 
$sql = "SELECT Category.CategoryID AS CategoryID, Category.CategoryName, Manufacturer.ManufacturerID AS ManufacturerID, Manufacturer.ManufacturerName, ManufacturerCategory.ManufacturerCategoryID, ManufacturerCategory.ManufacturerID, ManufacturerCategory.CategoryID FROM Category, Manufacturer, ManufacturerCategory WHERE Category.CategoryID = ManufacturerCategory.CategoryID AND Manufacturer.ManufacturerID = ManufacturerCategory.ManufacturerID AND Manufacturer.ManufacturerID = '".$ManufacturerID."' Order by Category.CategoryName";
echo $sql;
$rs=mysql_query($sql);
 
Doing this will show you your full completed query with all values inserted. You can then look that over and figure out what is wrong. Obviously the query is executing correctly now, but mysql is obviously not returning any results. I do not know what your table structure is and what data you have in them, so I cannot take you much further. Once the code I posted echoes/prints out the query, copy and paste it into mysql or phpMyAdmin and modify it till you get the results you are looking for. You can then edit the $sql line to reflect the changes you made. Once you have it working, you can remove or comment out the "echo $sql;"
mikeyboy
Forum Newbie
Posts: 13
Joined: Sat Jun 07, 2008 8:19 am

Re: PHP site problems

Post by mikeyboy »

Right i managed to figure out most of the problems, although one of my field names has gone awol in transit hehe, so its just saying PaintingName instead of the actual name. Doesnt really matter though as further down it is actually printing the name.

I'm more concerned with the form functionality now

Code: Select all

 
<?php
$connformat=mysql_connect("localhost","XXXX","XXXX");
mysql_select_db("XXXX",$connformat);
$rsformat=mysql_query("SELECT DISTINCT PaintingDetail.FormatID as FormatID, PaintingDetail.PaintingID, Format.FormatID, Format.FormatName FROM PaintingDetail, Format WHERE PaintingDetail.FormatID = Format.FormatID AND PaintingDetail.PaintingID = ".$PaintingID." ORDER BY PaintingDetail.FormatID");
if ($_POST["postback"]=="")
{
   if ($rsformat) { //This will check if the query failed or not
      if (mysql_num_rows($rsformat) > 0) {  //This will check if there were any results returned from the query
         while ($row = mysql_fetch_array($rsformat)) {
         print "<option value='".$rsformat['FormatID']."'>".$rsformat['FormatName']."</option>";
                                                 }
                                     }
             }
}
 else
{
  if ($rsformat)
  {
        if (mysql_result($rsformat,"FormatID")==$_POST["Format"])
        {
            print "<option value='".$rsformat['FormatID']."' selected='selected'>".$rsformat['FormatName']."</option>";
            $FormatID=mysql_result($rsformat,"FormatID");
            $FormatName=mysql_result($rsformat,"FormatName");
        }
        else
        {
            print "<option value='".mysql_result($rsformat,"FormatID")."'>".mysql_result($rsformat,"FormatName")."</option>";
         } 
   }
    while($row = mysql_fetch_array($rsformat));
}
mysql_close($connformat);
 
?>
 
That should be populating the dropdown box with the contents of the FormatName column from the table Format, located in my database. Again this is what i have come up with after trying to convert from odbc syntax.

I've included the old excerpt below (before conversion - with the old odbc syntax), in case i have taken out anything vital.

Code: Select all

 
<tr align="left">
 
  <td><select name="Format" onchange="submitform(form)">
    <?php
$connformat=odbc_connect('test','','');
$sqlformat="SELECT DISTINCT PaintingDetail.FormatID as FormatID, PaintingDetail.PaintingID, Format.FormatID, Format.FormatName FROM PaintingDetail, Format WHERE PaintingDetail.FormatID = Format.FormatID AND PaintingDetail.PaintingID = ".$PaintingID." ORDER BY PaintingDetail.FormatID"; 
 
$rsformat=odbc_exec($connformat,$sqlformat);
 
if ($_POST["postback"]=="")
{
      do 
    {
        print "<option value='".odbc_result($rsformat,"FormatID")."'>".odbc_result($rsformat,"FormatName")."</option>";
     } 
     while(odbc_fetch_row($rsformat));
}
 else
{
  do 
  {
 
        if (odbc_result($rsformat,"FormatID")==$_POST["Format"])
        {
            print "<option value='".odbc_result($rsformat,"FormatID")."' selected='selected'>".odbc_result($rsformat,"FormatName")."</option>";
            
            $FormatID=odbc_result($rsformat,"FormatID");
            $FormatName=odbc_result($rsformat,"FormatName");
        }
        else
        {
            print "<option value='".odbc_result($rsformat,"FormatID")."'>".odbc_result($rsformat,"FormatName")."</option>";
         } 
   }
    while(odbc_fetch_row($rsformat));
} 
odbc_close($connformat);
 
?>
  </select>
    &nbsp;
    <input type="button" value="Select" onclick="submitform(form)" /></td><td align="center">
 
mikeyboy
Forum Newbie
Posts: 13
Joined: Sat Jun 07, 2008 8:19 am

Re: PHP site problems

Post by mikeyboy »

Still no joy with this. It's a very long page of code with multiple dropdowns and i just can't figure the correct way to deploy them and the postback requirements.

Basically i need it to display the contents of FormatName (from the Format table) in the first dropdown. When the user chooses and hits the select button, it should show the style dropdown (populated from the Style table). Likewise when the user chooses a Style, they should then be shown the Size dropdown (populated by the Size table).

As each item is chosen it should be printed separately underneath using the print statements.

Finally under that it should use the 3 selected pieces of data to calculate the price (from the Price table)

I have commented out some of the POST code at the top as it refuses to work and i've added in definition of StyleID,StyleName,SizeID .. etc etc otherwise they are not recognised.

All of my linking and select statements seem to work fine, but these dropdowns have me stumped.

Any suggestions appreciated.

Code: Select 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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link rel="stylesheet" href="Test.css" type="text/css" />
<script language="JavaScript">
<!--                
function submitform(form)
{
    
     Paintingform.postback.value = "posted";
     form.submit();
}
//-->
</script>
</head>
 
<body>
<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
 
$CategoryID=$_GET["CategoryID"];
$CategoryName=$_GET["CategoryName"];
$GroupID=$_GET["GroupID"];
$Group=$_GET["Group"];
$ManufacturerID=$_GET["ManufacturerID"];
$ManufacturerName=$_GET["ManufacturerName"];
$ManufacturerCategoryID=$_GET["ManufacturerCategoryID"];
$PaintingID=$_GET["PaintingID"];
//$PaintingName=$_GET["PaintingName"];
 
echo "Microsoft.Jet.OLEDB.4.0";
$conn=mysql_connect("localhost","XXXX","XXXX");
mysql_select_db("XXXX",$conn);
 
$Style = ("Style");
$StyleID = ("StyleID");
$StyleName = ("StyleName");
$SizeID = ("SizeID");
$SizeName = ("SizeName");
$FormatID = ("FormatID");
$FormatName = ("FormatName");
//$PaintingName =("PaintingName");
$ImgName = ("ImgName");
$Price = ("Price");
 
/*
if ($_POST["Format"]!="")
{
  $formatID=$_POST["Format"];
} 
if ($_POST["Style"]!="")
{
  $styleID=$_POST["Style"];
} 
if ($_POST["Size"]!="")
{
   $SizeID=$_POST["Size"];
} 
 */
 
if ($_GET["PaintingID"]=="")
{
  $PaintingID=$_POST["PaintingID"];
}
  else
{
  $PaintingID=$_GET["PaintingID"];
}
 
// $rs is of type "ADODB.recordset"
$rs=mysql_query("SELECT Paintings.PaintingID, Paintings.PaintingName, Paintings.ManufacturerCategoryID, Paintings.ImgName, Paintings.Price, Paintings.GroupID FROM Paintings WHERE Paintings.PaintingID = '".$PaintingID."'");
$PaintingName =("PaintingName");
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0" cool gridx="8" gridy="8" showgridx showgridy usegridx usegridy><tr height="8"><td align="center"><img src="images/CGLLogoSmall.jpg"></td></tr></table>
                      
<hr />
<br />
<?php $row = mysql_fetch_array($rs) ?>
<table align="center">
<tr>
  <td align="center" class="heading"><a href="Gallery.php" class="heading">Gallery</a> - <a href="Group.php?Group=<?php print $Group;?>&GroupID=<?php print $GroupID;?>" class="heading"><?php print $Group;?> Prints</a> - <a href="Categories.php?CategoryID=<?php print $CategoryID;?>&CategoryName=<?php print $CategoryName;?>&Group=<?php print $Group;?>&GroupID=<?php print $GroupID;?>&ManufacturerID=<?php print $ManufacturerID;?>&ManufacturerName=<?php print $ManufacturerName;?>&ManufacturerCategoryID=<?php print $ManufacturerCategoryID;?>" class="heading">
    <?php print $ManufacturerName;?></a> - <a href="Paintings.php?CategoryID=<?php print $CategoryID;?>&CategoryName=<?php print $CategoryName;?>&Group=<?php print $Group;?>&GroupID=<?php print $GroupID;?>&ManufacturerID=<?php print $ManufacturerID;?>&ManufacturerName=<?php print $ManufacturerName;?>&ManufacturerCategoryID=<?php print $ManufacturerCategoryID;?>" class="heading"><?php print $CategoryName;?></a> - <?php print $row['PaintingName']; ?></td></tr>
 
</table>
<br /><br />
 
 
<table align="center" border="0">
<tr><td width="250px" align="left"><img src="images/<?php print $row['ImgName'];?>-large.gif" border="0" /></td>
<td valign="top" align="left">
<strong><?php   print $row['PaintingName'];?></strong><br /><br />
<?php   print $ManufacturerName;?><br /><br />
<?php   print $CategoryName;?><br /><br />
</td></tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr><td colspan="2" valign="bottom" align="left">
</td></tr>
 
<?php 
mysql_close($conn);
?>
 
</table>
<?php 
$conn3=mysql_connect("localhost","XXXX","XXXX");
mysql_select_db("XXXX",$conn3);
$rs3=mysql_query("SELECT PaintingDetail.PaintingDetailID, PaintingDetail.PaintingID, PaintingDetail.FormatID, PaintingDetail.StyleID, PaintingDetail.SizeID, PaintingDetail.Price FROM PaintingDetail WHERE PaintingDetail.PaintingID = ".$PaintingID." ORDER BY PaintingDetail.FormatID");?>
 
<p class="subheading">Please select the Format, Style and Size below</p>
<form name="Paintingform" method="post" id="Paintingform"><input type="hidden" name="PaintingID" value="<?php if ($_GET["PaintingID"]!="")
 
{
  print $_GET["PaintingID"];
}
  else
{
  print $_POST["PaintingID"];} ?> " /><input type="hidden" name="postback" value="<?php print $_POST["postback"];?>" /><input type="hidden" name="test" value="<?php print $StyleID;?>" />
<table align="center" width="800px" border="1"><tr><td><table border="0" class="heading" width="100%"><tr><td align="left" width="25%"><strong>Format</strong></td><td align="center" width="25%"><strong>
Style</strong>
</td>
<td align="right" width="25%"><strong>Size</strong></td>
 
</tr>
<tr><td colspan="4">&nbsp;</td></tr>
 
<tr align="left">
 
  <td><select name="Format" onchange="submitform(form)">
 
<?php
$connformat=mysql_connect("localhost","XXXX","XXXX");
mysql_select_db("XXXX",$connformat);
$rsformat=mysql_query("SELECT DISTINCT PaintingDetail.FormatID as FormatID, PaintingDetail.PaintingID, Format.FormatID, Format.FormatName FROM PaintingDetail, Format WHERE PaintingDetail.FormatID = Format.FormatID AND PaintingDetail.PaintingID = '".$PaintingID."' ORDER BY PaintingDetail.FormatID");
if ($_POST["postback"]=="")
{
   if ($rsformat) { //This will check if the query failed or not
      if (mysql_num_rows($rsformat) > 0) {  //This will check if there were any results returned from the query
         while ($row = mysql_fetch_array($rsformat)) {
         print "<option value='".$rsformat['FormatID']."'>".$rsformat['FormatName']."</option>";
                                                 }
                                     }
             }
}
 else
{
  if ($rsformat)
  {
        if (mysql_result($rsformat,"FormatID")==$_POST["Format"])
        {
            print "<option value='".$rsformat['FormatID']."' selected='selected'>".$rsformat['FormatName']."</option>";
            $FormatID=mysql_result($rsformat,"FormatID");
            $FormatName=mysql_result($rsformat,"FormatName");
        }
        else
        {
            print "<option value='".mysql_result($rsformat,"FormatID")."'>".mysql_result($rsformat,"FormatName")."</option>";
         } 
   }
    while($row = mysql_fetch_array($rsformat));
}
mysql_close($connformat);
 
?>
  </select>
    &nbsp;
    <input type="button" value="Select" onclick="submitform(form)" /></td><td align="center">
 
    <td><select name="Style" onchange="submitform(form)">
<?php
$connstyle=mysql_connect("localhost","XXXX","XXXX");
mysql_select_db("XXXX",$connstyle);
$rsstyle=mysql_query("SELECT DISTINCT PaintingDetail.StyleID AS StyleID, PaintingDetail.PaintingID, PaintingDetail.FormatID, Style.StyleID, Style.StyleName FROM PaintingDetail, Style WHERE PaintingDetail.StyleID = Style.StyleID AND PaintingDetail.PaintingID = ".$PaintingID." AND PaintingDetail.FormatID = '".$FormatID."' ORDER BY PaintingDetail.StyleID");
If ($_POST["Style"]=="")
{
     if ($rsstyle) { //This will check if the query failed or not
      if (mysql_num_rows($rsstyle) > 0) {  //This will check if there were any results returned from the query
         while ($row = mysql_fetch_array($rsstyle)) {
         $StyleID=mysql_result($rsstyle,"StyleID");
 
       print "<option value='".$rsformat['StyleID']."'>".$rsstyle['StyleName']."</option>";
                                                 }
                                     }
             }
}
else
{
 
  //if (!($rsstyle==0))
  if(mysql_fetch_row($rsstyle))
  
  {
 
    print "<select name='Style' onchange='submitform(form)'>";
    do 
    {
 
        if (mysql_result($rsstyle,"StyleID")==$_POST["Style"])
        {
 
            print "<option value='".mysql_result($rsstyle,"StyleID")."' selected='selected'>".mysql_result($rsstyle,"StyleName")."</option>";
 
            $StyleID=$rsstyle['StyleID'];
            $StyleName=$rsstyle['StyleName'];
        }
        else
        {
 
            print "<option value='".mysql_result($rsstyle,"StyleID")."'>".mysql_result($rsstyle,"StyleName")."</option>";
            $StyleID=$_POST["Style"];
        } 
 
                
    } 
 while ($row = mysql_fetch_array($rsstyle));
    
    
    print "</select>&nbsp;<input type='button' value='Select' onclick='submitform(form)' />";
 
  }
else
  {
 
 
    $StyleID="NA";
    $StyleName="N/A";
    print $StyleName;
  } 
 
 
} 
 
mysql_close($connstyle);
 
//styleID = request.form("Style")
?>
  </select>
    &nbsp;
    <input type="button" value="Select" onclick="submitform(form)" /></td><td align="center">
 
    <td><select name="Size" onchange="submitform(form)">
                </td>
<td align="right">
 
<?php
// $connsize is of type "ADODB.Connection"
$connsize=mysql_connect("localhost","XXXX","XXXX");
mysql_select_db("XXXX",$connsize);
 
if ($StyleID!="NA")
{
 
$rssize=mysql_query("SELECT DISTINCT PaintingDetail.SizeID as SizeID, PaintingDetail.PaintingID, PaintingDetail.FormatID, PaintingDetail.StyleID, [Size].SizeID, [Size].SizeName FROM PaintingDetail, [Size] WHERE PaintingDetail.SizeID = [Size].SizeID AND PaintingDetail.PaintingID = ".$PaintingID." AND PaintingDetail.FormatID = '".$FormatID."' AND PaintingDetail.StyleID = '".$StyleID."' ORDER BY PaintingDetail.SizeID");
 
}
else
{
 
$rssize=mysql_query("SELECT DISTINCT PaintingDetail.SizeID as SizeID, PaintingDetail.PaintingID, PaintingDetail.FormatID, PaintingDetail.StyleID, [Size].SizeID, [Size].SizeName FROM PaintingDetail, [Size] WHERE PaintingDetail.SizeID = [Size].SizeID AND PaintingDetail.PaintingID = ".$PaintingID." AND PaintingDetail.FormatID = '".$FormatID."'");
 
{
     if ($rssize) { //This will check if the query failed or not
      if (mysql_num_rows($rssize) > 0) {  //This will check if there were any results returned from the query
         while ($row = mysql_fetch_array($rssize)) {
 
 
 
       print "<option value='".$rssize['SizeID']."'>".$rssize['SizeName']."</option>";
            $SizeID=$rssize['SizeID'];
                                                 }
                                     }
             }
}
    print "<select name='Size' onchange='submitform(form)'>";
        do 
        {
 
            if (mysql_result($rssize,"SizeID")==$_POST["Size"])
            {
 
                print "<option value='".mysql_result($rssize,"SizeID")."' selected='selected'>".mysql_result($rssize,"SizeName")."</option>";
                $SizeID=$rssize['SizeID'];
                $SizeName=$rssize['SizeName'];
 
            }
            else
            {
 
                print "<option value='".mysql_result($rssize,"SizeID")."'>".mysql_result($rssize,"SizeName")."</option>";
                
            } 
        }
        while(odbc_fetch_row($rssize));
 
                   print "</select>&nbsp;<input type='button' value='Select' onclick='submitform(form)' />";
                
}
 
 
mysql_close($connsize);?>
 
</td>               
</tr>
<tr><td colspan="3">&nbsp;</td></tr>
<?php print "<table width='100%' border='0' class='subheading'><tr><td>";
print "<tr><td width='30%' align='left'>&nbsp;</td><td align='left'>Format:</td>";
 
if ($FormatName=="")
{
 
  print "<td align='left'>Please select a Format</td><td width='30%'>&nbsp;</td></tr>";
}
  else
{
  print "<td align='left'>".$FormatName."</td><td width='30%'>&nbsp;</td></tr>";
 // print "<td align='left'>".$FormatID."</td><td width='30%'>&nbsp;</td></tr>";
} ?>
 
<?php print "<tr><td width='30%' align='left'>&nbsp;</td><td align='left'>Style:</td>";?>
<?php if ($StyleName=="")
{
 
  print "<td align='left'>Please select a Style</td><td width='30%'>&nbsp;</td></tr>";
}
  else
{
 
  print "<td align='left'>".$StyleName."</td><td width='30%'>&nbsp;</td></tr>";
  //print "<td align='left'>".$StyleID."</td><td width='30%'>&nbsp;</td></tr>";
} 
 
?>
 
<?php print "<tr><td width='30%' align='left'>&nbsp;</td><td align='left'>Size:</td>";?>
<?php if ($SizeName=="")
{
 
  print "<td align='left'>Please select a Size</td><td width='30%'>&nbsp;</td></tr>";
}
  else
{
 
  print "<td align='left'>".$SizeName."</td><td width='30%'>&nbsp;</td></tr>";
  //print "<td align='left'>".$SizeID."</td><td width='30%'>&nbsp;</td></tr>";
} ?>
<?php print "<tr><td colspan='4'>&nbsp;</td></tr>";?>
 
<?php 
 
// $connprice is of type "ADODB.Connection"
$connprice=mysql_connect("localhost","XXXX","XXXX");
mysql_select_db("XXXX",$connprice);
 
if ($StyleID == "NA") 
{
 
$rsprice=("SELECT PaintingDetail.Price, PaintingDetail.SizeID, PaintingDetail.PaintingID, PaintingDetail.FormatID, PaintingDetail.StyleID FROM PaintingDetail WHERE PaintingDetail.PaintingID = ".$PaintingID." AND PaintingDetail.FormatID = '".$FormatID."' AND PaintingDetail.SizeID = '".$SizeID."' ORDER BY PaintingDetail.Price");
 
}
  else
{
 
$rsprice=("SELECT PaintingDetail.Price, PaintingDetail.SizeID, PaintingDetail.PaintingID, PaintingDetail.FormatID, PaintingDetail.StyleID FROM PaintingDetail WHERE PaintingDetail.PaintingID = ".$PaintingID." AND PaintingDetail.FormatID = '".$FormatID."' AND PaintingDetail.StyleID = '".$StyleID."' AND PaintingDetail.SizeID = '".$SizeID."' ORDER BY PaintingDetail.Price");
 
} 
 
//if ($_POST["postback"]=="")
//{
 
 
//}
//  else
{
 
//  if (mysql_fetch_row($rsprice))
//
//  {
//      $Price=$rsprice['Price'];
//
//  }
//    else
//  {
 
 $Price="crap";
//response.Write("No Price")
  } 
 
?>
 
<?php if ($FormatName=="" || $StyleName=="" || $SizeName=="")
{
 
}
  else
{
 
  print "<tr><td width='30%' align='left'>&nbsp;</td><td align='left'><strong>Price:</strong></td><td align='left'><strong>&pound;".number_format($Price)."</strong></td><td width='30%'>&nbsp;</td></tr>";
 //print $sqlprice;
?>
<?php   print "</tr></td></table>";?>
<?php  
  mysql_close($connprice);
 
} ?>
 
</table></td></tr></table>
<?php //if request.form("Format") <> "" and request.form("Style") <> "" and request.Form("Size") <> "" then
//response.Write("<br /><input type='button' value='Refresh Price' onclick='submitform(form)' />") 
//end if
?>
 
</form>
<hr />
</body>
</html>
 
I apologise unreservedly for the length of that, but it seemed best to let you see everything going on in the page than snippets.

Many Thanks
Mikey
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: PHP site problems

Post by dbemowsk »

What does the "if ($_POST["postback"]=="")" do? From what I can tell it appears to be checking if the form was posted. If that is the case I would rewrite this bit like this.

Code: Select all

 
<?php
$connformat=mysql_connect("localhost","XXXX","XXXX");
mysql_select_db("XXXX",$connformat);
$rsformat=mysql_query("SELECT DISTINCT PaintingDetail.FormatID as FormatID, PaintingDetail.PaintingID, Format.FormatID, Format.FormatName FROM PaintingDetail, Format WHERE PaintingDetail.FormatID = Format.FormatID AND PaintingDetail.PaintingID = ".$PaintingID." ORDER BY PaintingDetail.FormatID");
if ($rsformat) { //This will check if the query failed or not
  if (mysql_num_rows($rsformat) > 0) {  //This will check if there were any results returned from the query
    while ($row = mysql_fetch_array($rsformat)) {
      $selected = (isset($_POST["Format"]) && $row['FormatID'] == $_POST["Format"]) ? " selected='selected'" : "";
      print "<option value='".$rsformat['FormatID'].$selected.">".$rsformat['FormatName']."</option>";
    }
  }
} else {
  print "Query error: ".mysql_error();
}
mysql_close($connformat);
 
?>
 
If you are curious about the line

Code: Select all

 
$selected = (isset($_POST["Format"]) && $row['FormatID'] == $_POST["Format"]) ? " selected='selected'" : "";
 
This is the ternary operator format of an if...then...else statement. It is equivalent to this.

Code: Select all

 
if (isset($_POST["Format"]) && $row['FormatID'] == $_POST["Format"]) {
  $selected =  " selected='selected'";
} else {
  $selected = "";
}
 
Ternary operator format is something you should learn if you do not know it. As you can see it is significantly shorter than the standard if the format.

I am giving you the pieces to the puzzle, you just need to figure out where to put them in your situation. If you are creating multiple select boxes, I would write a function that does that where you can pass it some parameters and have it spit out the code for the needed select box. That is just proper coding and a way to eliminate code redundancy. I have an article on eliminating code redundancy on my website. Just click the link in my signature.
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: PHP site problems

Post by dbemowsk »

Another note...

You only need to do one database connection (mysql_connect...) per page. I am seeing "$conn = mysql_connect...", "$conn2 = mysql_connect...", "$conn3 = mysql_connect...", etc... This is not necessary and totally redundant unless you are connecting to different databases which I doubt. Do the one connection and use that throughout your page.
mikeyboy
Forum Newbie
Posts: 13
Joined: Sat Jun 07, 2008 8:19 am

Re: PHP site problems

Post by mikeyboy »

Hi Dbemowsk,

Just tried that chunk of code in place of my format section and it still displays a blank dropdown not populated by anything let alone the database ... aaargh
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: PHP site problems

Post by dbemowsk »

Then I would check your database query like I showed you previously using the $sql variable. Also, you don't have anything in there showing if the query returned results or not, but that would be my guess.

Slow down and think this all through logically when you are doing the rewrite. The things I have showed you here should get you on the right track, you just need to do the logical thinking.
Post Reply