Jump to content

Beginners syntax and loop questions - help please!


Digital Wallfare

Recommended Posts

Hi everyone,

 

I have some problems with php code. I'm currently in the middle of learning php, but these problems have got me stuck fast. But also have a question regarding looping that i'll ask in the same thread:

 

Firstly, I keep getting various syntax errors with my table:

 

PHP Code:

echo "
<table border='1' bordercolor='#FFCC00'>
<style='background-color:#FFFFFF' width='800' cellpadding='3' cellspacing='3'>
<tr>
<td width='200' align='left' rowspan=5>Image</td>
</tr>
<tr>
<td width='573' align='left'>'Name:$row_details['MerchantName']</td>
</tr>
<tr>
<td height='27' align='left'>Telephone:$row_details['Telephone']</td>
</tr>
<tr>
<td height='27' align='left'>Website:$row_details['Website']</td>
</tr>
<tr>
<td height='54' align='left'>Description:$row_details['Description']</td>
</tr>

</html></body></table>"; 

it keeps telling me its expecting ";" or "," in various place along with T_Lnumber errors as i mentioned, i'm brand new to coding so im not sure whats happening there, the books and forums I've read dont point to these errors.

 

Also, I'm not sure if $row_details[''] is the correct code to display results from the database?

 

And the loop question I have is: Is it possible to get that little table above (once its working) to display 5 times per page. (and i suppose recognise that it need a new page if there more than 5 results)

 

Sorry this is a huge post but I've been trial and erroring for 2 days and its time to call in the pro's.

 

Sam

Link to comment
Share on other sites

Unfortunately, the php parser needs help finding the start and end of array variables when they are contained within a double-quoted string. One way to accomplish this is to surround array variables in {} when they are in a double-quoted string. This will allow you to leave in the quotes that are around the associative index names so you don't need to use different syntax for array variables inside and outside strings.

 

Also, I'm not sure if $row_details[''] is the correct code to display results from the database?

If $row_details['....'] is the result of an assignment of the value returned by a mysql_fetch_xxxxx() instruction, then yes. If you are having a problem, it woud take seeing the code that is execuiting the query up through the code you posted.

 

And the loop question I have is: Is it possible to get that little table above (once its working) to display 5 times per page. (and i suppose recognise that it need a new page if there more than 5 results)

That is referred to as pagination. There are countless examples posted around the Internet  - http://www.phpfreaks.com/tutorial/basic-pagination Short-version: You use a LIMIT clause in the query to determine which 'group' of 5 rows you want and setup links to cause the page to query for and then display the correct group of rows. The query itself returns the number of rows you want, so you just put your display code inside of a while() loop.

 

 

Link to comment
Share on other sites

Thank you very much for the help so far, I've followed the suggestions and modified my code, but i've encounters a pit fall:

$MerchantType = $_POST["MerchantType"];
$County = $_POST["County"];

$result = mysql_query("SELECT * FROM $tablename WHERE County = $County");

 

The variable $County isnt working and im not sure if it is a syntax related problem or something im doing wrong with the _POST statements.

 

Basically the _POST's are supposed to be getting information from a pair of drop down boxes on another page. And from there the SELECT array is supposed to find any entrys with said variables in them, Im adding them one at a time to make sure they work and have stumbled on the first one.

 

Am I doing this right?

 

Again, sorry for the really basic questions I honestly have tried to read the PHP documentation etc but need validation in what i've read and coded.

 

Sam

Link to comment
Share on other sites

A good way to pinpoint where something is wrong with your code is to have PHP help you as much as it can. For example, turning error reporting on fully, and displaying all errors can help a lot. If you don't already have error reporting turned on, you can add this at the beginning of your code

error_reporting(E_ALL);
ini_set("display_errors", 1);

 

Another good idea, is when you are dealing with queries, while developing/learning, add an "or" clause to the end of the query. For example

$query = mysql_query("my query") or trigger_error(mysql_error());

What that will do is show an error if the query failed.

 

Another good idea is to output variables that are troubling you. You would use echo, or print, or as aba said, print_r to show the variables. This way you can see what their value is at that point of the code, and you can compare that to what you expect it to be.

 

by the way, print_r is for objects (like arrays) and shows their structure, and the values of their different elements or attributes

 

It looks like this is the problem tho

$result = mysql_query("SELECT * FROM $tablename WHERE County = $County");

when you pass in a string to a query (when comparing columns to values), you need to surround it with single quotes. otherwise, mysql thinks its a variable, or command, and it becomes invalid sql syntax.

 

$result = mysql_query("SELECT * FROM $tablename WHERE County = '$County'");

see if that helps

Link to comment
Share on other sites

You may want to place your $_POST statement in IF's. Such as this:

if (isset($_POST['MerchantType']) && isset($_POST['County'])) {
$MerchantType = $_POST["MerchantType"];
$County = $_POST["County"];

$result = mysql_query("SELECT * FROM $tablename WHERE County = $County");
}

This will make it so that code will only be executed, if the page sends the $_POST. Very useful for debugging, then you can look at your dropdown box and see if the NAME is correct etc.

Link to comment
Share on other sites

Array ( )

 

Even I in my infinate newness can tall thats not good lol.

 

Any one able to help as to why my form isnt POSTing? I have:

 

action="<?php echo $_SERVER['PHP_SELF'] ?>"

 

as my form action which i read was the correct thing for a form on the same page. I could be wrong though.

 

Anyone?

you can remove the action completely. a form's default action is itself.

also, are you using method="POST"? why dont you put post your entire form code, and the code to process it all in one post. this is like a game of tag.

Link to comment
Share on other sites

Apologies, you're right im sorry.

 

Here is my full code:

  <div id="rightbottom">
<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);

    $hostname = "localhost";
    $username = "######";
    $password = "######";
    $database = "######";
    $tablename = "######";
    $connection = mysql_connect($hostname, $username, $password);
    if (!$connection) {
       die("A connection to the server could not be established");
    }

    mysql_select_db($database) or die("Database could not be selected!");


if (isset($_POST['MerchantType']) && isset($_POST['County'])){
    $MerchantType = $_POST["MerchantType"];
    $County = $_POST["County"];
$result = mysql_query("SELECT * FROM $tablename WHERE County = '$County' AND MerchantType = '$MerchantType'");

while ($row_details = mysql_fetch_array($result))
{								
echo '
<table border="1" bordercolor="#FFCC00">
<style="background-color:#FFFFFF" width="800" cellpadding="3" cellspacing="3">
<tr>
<td width="200" align="left" rowspan="5">Image</td>
</tr>
<tr>
<td width="573" align="left">Name: '.$row_details['MerchantName'].'</td>
</tr>
<tr>
<td height="27" align="left">Telephone: '.$row_details['Telephone'].'</td>
</tr>
<tr>
<td height="27" align="left">Website: '.$row_details['Website'].'</td>
</tr>
<tr>
<td height="54" align="left">Description: '.$row_details['Description'].'</td>
</tr>
</table>';
}
}else{
?>
<form><form name="Results Filter" action="<?php $_SERVER['PHP_SELF']?>" method="POST"> Select Category:
<select name="MerchantType">
  <option value="antiques">Antiques</option>
  <option value="Bicycles">Bicycles</option>
  <option value="B+B's/Hotels">B+B's/Hotels</option>
  <option value="Beauty Salons/Hairdressers">Beauty Salons/Hairdressers</option>
  <option value="Bookshops">Bookshops</option>
  <option value="Bakers/Butchers">Bakers/Butchers</option>
  <option value="Car Parts/Garages">Car Parts/Garages</option>
  <option value="Clothing">Clothing</option>
  <option value="DIY">DIY</option>
  <option value="Computers/Electronics">Computers/Electronics</option>
  <option value="Florists">Florists</option>
  <option value="Furniture/Beds">Furniture/Beds</option>
  <option value="Groceries">Groceries</option>
  <option value="Garden Centres">Garden Centres</option>
  <option value="Gifts/Stationary">Gifts/Stationary</option>
  <option value="Jewellers">Jewellers</option>
  <option value="Kitchen/Cookware">Kitchen/Cookware</option>
  <option value="Model/Hobby Shops">Model/Hobby Shops</option>
  <option value="Music">Music</option>
  <option value="Newsagents">Newsagents</option>
  <option value="Shoe Shops/Shoe Repairs">Shoe Shops/Shoe Repairs</option>
  <option value="Toys and Games">Toys and Games</option>
  <option value="Travel Agents">Travel Agents</option>
  <option selected>Antiques</option>
</select>
  County: <select name="County">
<option value="Carlow">Carlow</option>
<option value="Cavan">Cavan</option>
<option value="Clare">Clare</option>
<option value="Cork">Cork</option>
<option value="Donegal">Donegal</option>
<option value="Dublin">Dublin</option>
<option value="Galway">Galway</option>
<option value="Kerry">Kerry</option>
<option value="Kildare">Kildare</option>
<option value="Laois">Laois</option>
<option value="Leitrim">Leitrim</option>
<option value="Limerick">Limerick</option>
<option value="Longford">Longford</option>
<option value="Louth">Louth</option>
<option value="Meath">Meath</option>
<option value="Monaghan">Monaghan</option>
<option value="Offaly">Offaly</option>
<option value="Roscommon">Roscommon</option>
<option value="Sligo">Sligo</option>
<option value="Tipperary">Tipperary</option>
<option value="Waterford">Waterford</option>
<option value="Westmeath">Westmeath</option>
<option value="Wexford">Wexford</option>
<option value="Wicklow">Wicklow</option>
<option selected>Galway</option>
</select>
<input type="submit" value="Search" Name="submit1">
    
  </form>


<?php
}
print_r($_POST);
echo "</body></html>";
?>
</div>
</div>
</body>

 

Again, sorry for not doing this earlier,

 

Sam

Link to comment
Share on other sites

I removed the html and the mysql query part, but you can fill that in. It is working now. You had a loose '<form>' tag in there, which might have been the problem.

 

<div id="rightbottom">
<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);

if (isset($_POST['submit1'])){
    $MerchantType = $_POST["MerchantType"];
    $County = $_POST["County"];
print_r($_POST);
}else{
?>
<form name="Results Filter" action="<?php $_SERVER['PHP_SELF']?>" method="POST"> Select Category:
<select name="MerchantType">
  <option value="antiques">Antiques</option>
  <option value="Bicycles">Bicycles</option>
  <option value="B+B's/Hotels">B+B's/Hotels</option>
  <option value="Beauty Salons/Hairdressers">Beauty Salons/Hairdressers</option>
  <option value="Bookshops">Bookshops</option>
  <option value="Bakers/Butchers">Bakers/Butchers</option>
  <option value="Car Parts/Garages">Car Parts/Garages</option>
  <option value="Clothing">Clothing</option>
  <option value="DIY">DIY</option>
  <option value="Computers/Electronics">Computers/Electronics</option>
  <option value="Florists">Florists</option>
  <option value="Furniture/Beds">Furniture/Beds</option>
  <option value="Groceries">Groceries</option>
  <option value="Garden Centres">Garden Centres</option>
  <option value="Gifts/Stationary">Gifts/Stationary</option>
  <option value="Jewellers">Jewellers</option>
  <option value="Kitchen/Cookware">Kitchen/Cookware</option>
  <option value="Model/Hobby Shops">Model/Hobby Shops</option>
  <option value="Music">Music</option>
  <option value="Newsagents">Newsagents</option>
  <option value="Shoe Shops/Shoe Repairs">Shoe Shops/Shoe Repairs</option>
  <option value="Toys and Games">Toys and Games</option>
  <option value="Travel Agents">Travel Agents</option>
  <option selected>Antiques</option>
</select>
  County: <select name="County">
<option value="Carlow">Carlow</option>
<option value="Cavan">Cavan</option>
<option value="Clare">Clare</option>
<option value="Cork">Cork</option>
<option value="Donegal">Donegal</option>
<option value="Dublin">Dublin</option>
<option value="Galway">Galway</option>
<option value="Kerry">Kerry</option>
<option value="Kildare">Kildare</option>
<option value="Laois">Laois</option>
<option value="Leitrim">Leitrim</option>
<option value="Limerick">Limerick</option>
<option value="Longford">Longford</option>
<option value="Louth">Louth</option>
<option value="Meath">Meath</option>
<option value="Monaghan">Monaghan</option>
<option value="Offaly">Offaly</option>
<option value="Roscommon">Roscommon</option>
<option value="Sligo">Sligo</option>
<option value="Tipperary">Tipperary</option>
<option value="Waterford">Waterford</option>
<option value="Westmeath">Westmeath</option>
<option value="Wexford">Wexford</option>
<option value="Wicklow">Wicklow</option>
<option selected>Galway</option>
</select>
<input type="submit" value="Search" Name="submit1">
   
  </form>
<?php
}
?>

</div>
</div>
</body>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.