I am allowing the user to select a city and state and presenting them with a list of organizations from a MySQL table which is formatted by the code shown below. The user then selects a particular organization by clicking on the ID for their chosen oranization at which point I am using the json_encode function to prepare
my data for pick up by my application. I put the table formatting in its own function so that it is not shown on the page with the json data. I am getting the error shown below and I don't know why:

"Parse error: syntax error, unexpected '<' in /data/21/2/29/104/2029919/user/2222485/htdocs/citystate.php on line 39"

Line 39 is the line that begins with "<table border..."

<?php
function show_table() {
   <table border="1" cellspacing="2" cellpadding="2" style="background-color:#33990f;border-width:2px;width:100%;">
   <tr>
   <th><font face="Arial, Helvetica, sans-serif";font color="#FFFAFA">Organization Name</font></th>
   <th><font face="Arial, Helvetica, sans-serif";font color="#FFFAFA">ID</font></th>
   </tr>
   </table>
}
?>

Recommended Answers

All 15 Replies

Hey this should be like this

<?php function show_table() { ?>
<table border="1" cellspacing="2" cellpadding="2" style="background-color:#33990f;border-width:2px;width:100%;">
   <tr>
   <th><font face="Arial, Helvetica, sans-serif";font color="#FFFAFA">Organization Name</font></th>
   <th><font face="Arial, Helvetica, sans-serif";font color="#FFFAFA">ID</font></th>
   </tr>
   </table>
<?php } ?>

Hope this will help you

I modified as suggested and now I receive the error:

Fatal error: Call to undefined function show_table() in /data/21/2/29/104/2029919/user/2222485/htdocs/citystate.php on line 35

Line 35 is the line where I call the function with

show_table();

Hey,

can you paste the complete file then we can found where the problem exactly...

Here is the entire php file:

<html> 
<head> 
<title>Locate Organizations Form</title> 
<h3 style="background-color:#33990f"<FONT COLOR="#FFFAFA">Organizations</FONT></h3> 
</head>
<body style="background-color:#33990f"> 

<?php 

show_organizations();
select_oneorg();

function show_organizations() {

// connect include
require ("connect.php");
$query = "SELECT orgname, org_id FROM organizations WHERE ";
    if (strlen($_GET['orgcity']) > 0)
	    $query .= " organizations.orgcity LIKE '%{$_GET['orgcity']}%' AND ";
$query .= "organizations.orgstate = '{$_GET['orgstate']}'";
/* Temporary ECHO of the $sql string */ 
/* echo $query; */
//
// ensure you have retrieved results of query
//
$result = mysql_query($query)or die(mysql_error());
    if (!$result) { 
      echo("<p>Error performing query: " . mysql_error() . "</p>"); 
      exit(); 
    } 
?>
<div>
<table border="1" cellspacing="2" cellpadding="2" style="background-color:#33990f;border-width:2px;width:100%;">
<tr>
<th><font face="Arial, Helvetica, sans-serif";font color="#FFFAFA">Organization Name</font></th>
<th><font face="Arial, Helvetica, sans-serif";font color="#FFFAFA">ID</font></th>
</tr>

<?php
//
// put results into table
//
    while ($row = mysql_fetch_assoc($result))
    {
        echo("<tr>\n<td>" . $row["orgname"] . "</td>"); 
        echo "<td>";
        echo "<a href = '?varID=".$row['org_id']."'>".$row['org_id']."</td></a>";  
    }
}
?>
</table>
</div>
<?php

function select_oneorg() {

if (isset($_GET['varID'])){
    $query = "SELECT * FROM organizations WHERE org_id = {$_GET['varID']}";

    /* Temporary ECHO of the $sql string */ 
    /* echo $query; */
    $result = mysql_query($query)or die(mysql_error());
    if (!$result) { 
      echo("<p>Error performing query: " . mysql_error() . "</p>"); 
      exit(); 
    } 
    while ($row = mysql_fetch_assoc($result))
    {
	    echo(json_encode($row));
    }
}
}
/* Closes Connection to MySQL server */ 
mysql_close(); 
exit();
?>

You need to ecbo the table for it to works:

<?php
function show_table() {
   echo "<table border=\"1\" cellspacing=\"2\" cellpadding=\"2\" style=\"background-color:#33990f;border-width:2px;width:100%;\">
   <tr>
   <th><font face=\"Arial, Helvetica, sans-serif\";font color=\"#FFFAFA\">Organization Name</font></th>
   <th><font face=\"Arial, Helvetica, sans-serif\";font color=\"#FFFAFA\">ID</font></th>
   </tr>
   </table>";
}
?>

Make sure you escape any speech marks within the code.

I incorporated the suggested changes and received the following error (line 35 is the "show_table(); statement":

Fatal error: Call to undefined function show_table() in /data/21/2/29/104/2029919/user/2222485/htdocs/citystate.php on line 35

Could you give us the section of code that calls the function?

Below is the entire php file. Thanks for your help.

<html> 
<head> 
<title>Locate Organizations Form</title> 
<h3 style="background-color:#33990f"<FONT COLOR="#FFFAFA">Organizations</FONT></h3> 
</head>
<body style="background-color:#33990f"> 

<?php 

show_organizations();
select_oneorg();

function show_organizations() {

// connect include
require ("connect.php");
$query = "SELECT orgname, org_id FROM organizations WHERE ";
    if (strlen($_GET['orgcity']) > 0)
	    $query .= " organizations.orgcity LIKE '%{$_GET['orgcity']}%' AND ";
$query .= "organizations.orgstate = '{$_GET['orgstate']}'";
/* Temporary ECHO of the $sql string */ 
/* echo $query; */
//
// ensure you have retrieved results of query
//
$result = mysql_query($query)or die(mysql_error());
    if (!$result) { 
      echo("<p>Error performing query: " . mysql_error() . "</p>"); 
      exit(); 
    } 
show_table();
?>
<?php
function show_table() { ?>
   <table border="1" cellspacing="2" cellpadding="2" style="background-color:#33990f;border-width:2px;width:100%;">
   <tr>
   <th><font face="Arial, Helvetica, sans-serif";font color="#FFFAFA">Organization Name</font></th>
   <th><font face="Arial, Helvetica, sans-serif";font color="#FFFAFA">ID</font></th>
   </tr>
   </table>
<?php
}
?>

<?php
//
// put results into table
//
    while ($row = mysql_fetch_assoc($result))
    {
        echo("<tr>\n<td>" . $row["orgname"] . "</td>"); 
        echo "<td>";
        echo "<a href = '?varID=".$row['org_id']."'>".$row['org_id']."</td></a>";  
    }
}
?>
<?php

function select_oneorg() {

if (isset($_GET['varID'])){
    $query = "SELECT * FROM organizations WHERE org_id = {$_GET['varID']}";

    /* Temporary ECHO of the $sql string */ 
    /* echo $query; */
    $result = mysql_query($query)or die(mysql_error());
    if (!$result) { 
      echo("<p>Error performing query: " . mysql_error() . "</p>"); 
      exit(); 
    } 
    while ($row = mysql_fetch_assoc($result))
    {
	    echo(json_encode($row));
    }
}
}
/* Closes Connection to MySQL server */ 
mysql_close(); 
exit();
?>

Hey,

You have called a custom function show_table(); before declaring what it is. Remember this is a php script - it will be parsed line-by line.

s
how_table();
?>
<?php
function show_table() { ?>
   <table border="1" cellspac...

I agree with a previous comment that suggested echoing your table, if only to make things easier to for you to read. I don't know why someone suggested using a custom function to simply echo a table as part of another function.

Change this:

show_table();
?>
<?php
function show_table() { ?>
   <table border="1" cellspacing="2" cellpadding="2" style="background-color:#33990f;border-width:2px;width:100%;">
   <tr>
   <th><font face="Arial, Helvetica, sans-serif";font color="#FFFAFA">Organization Name</font></th>
   <th><font face="Arial, Helvetica, sans-serif";font color="#FFFAFA">ID</font></th>
   </tr>
   </table>
<?php

to this:

echo "<table border=\"1\" cellspacing=\"2\" cellpadding=\"2\" style=\"background-color:#33990f;border-width:2px;width:100%;\">
   <tr>
   <th><font face=\"Arial, Helvetica, sans-serif\";font color=\"#FFFAFA\">Organization Name</font></th>
   <th><font face=\"Arial, Helvetica, sans-serif\";font color=\"#FFFAFA\">ID</font></th>
   </tr>
   </table>";

and report back :)

Implemented suggested solution and received the following error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /data/21/2/29/104/2029919/user/2222485/htdocs/citystate.php on line 55

Hey,

mysql_fetch_assoc needs to be called on a mysql_query('SELECT...') result.

Also, I just noticed you have a h3 tag in the <head> section of your html, you only put meta data (including title, css/js links etc) in your <head> and actual content in the <body> section.

Please post your code so I can be sure of the problem, we should have this fixed once the SQL issue is resolved. What is this project for btw?

The project will go up on a web page at my site for helping people find organizations they need to contact.

The full code is shown on page one of this discussion.

Thanks for your help.

Yeah if you are getting new errors since modifying your code it would be helpful to repost it. I would suspect the problem lies with your query string but can't be sure.

Try changing this:

$query = "SELECT orgname, org_id FROM organizations WHERE ";
    if (strlen($_GET['orgcity']) > 0)
	    $query .= " organizations.orgcity LIKE '%{$_GET['orgcity']}%' AND ";
$query .= "organizations.orgstate = '{$_GET['orgstate']}'";

to this:

$orgCity = $_GET['orgcity'];
$orgState = $_GET['orgstate'];

$query = "SELECT orgname, org_id FROM organizations WHERE ";

if (strlen($orgCity) > 0)
{
    $query .= " organizations.orgcity LIKE '%" . $orgCity . "%' AND ";
}

$query .= "organizations.orgstate = '" . $orgState . "'";

then test the query using something like this directly below the above code:

$queryRes = mysql_query($query);

while($queryAssocArray = mysql_fetch_assoc($queryRes))
{
    $queryAssocArray['orgname'];
}

If you can't get any further please post your (updated) complete code.

That should have been:

echo $queryAssocArray['orgname'];

inside the while loop btw, sorry :S

I am able to present the organizations for a user selected city and state and I am then able to "down select" using the id which presents me with the json encoded data for a single organization. The problem is that along with the json encoded data I am getting the table headings along with the grid. This means that when I try to grab the json data with an HttpResponse I get a "java.lang.StringIndexOutOfBoundsException" since I am essentially reading all of the HTML contained in the citystate.php file as well as the json data. Here is the code that works but presents the problem as just described.

<html> 
<head> 
<title>Locate Organizations Form</title> 
<h3 style="background-color:#33990f"<FONT COLOR="#FFFAFA">Organizations</FONT></h3> 
</head>
<body style="background-color:#33990f"> 

<?php 

show_organizations();
select_oneorg();

function show_organizations() {

// connect include
require ("connect.php");
$query = "SELECT orgname, org_id FROM organizations WHERE ";    
    if (strlen($_GET['orgcity']) > 0)	    
             $query .= " organizations.orgcity LIKE '%{$_GET['orgcity']}%' AND ";
$query .= "organizations.orgstate = '{$_GET['orgstate']}'";
/* Temporary ECHO of the $sql string */ 
/* echo $query; */
//
// ensure you have retrieved results of query
//
$result = mysql_query($query)or die(mysql_error());
    if (!$result) { 
      echo("<p>Error performing query: " . mysql_error() . "</p>"); 
      exit(); 
    } 

?>
<div>
<table border="1" cellspacing="2" cellpadding="2" style="background-color:#33990f;border-width:2px;width:100%;">
<tr>
<th><font face="Arial, Helvetica, sans-serif";font color="#FFFAFA">Organization Name</font></th>
<th><font face="Arial, Helvetica, sans-serif";font color="#FFFAFA">ID</font></th>
</tr>

<?php
//
// put results into table
//
    while ($row = mysql_fetch_assoc($result))
    {
        echo("<tr>\n<td>" . $row["orgname"] . "</td>"); 
        echo "<td>";
        echo "<a href = '?varID=".$row['org_id']."'>".$row['org_id']."</td></a>";  
    }
}
?>
</table>
</div>
<?php

function select_oneorg() {

if (isset($_GET['varID'])){
    $query = "SELECT * FROM organizations WHERE org_id = {$_GET['varID']}";

    /* Temporary ECHO of the $sql string */ 
    /* echo $query; */
    $result = mysql_query($query)or die(mysql_error());
    if (!$result) { 
      echo("<p>Error performing query: " . mysql_error() . "</p>"); 
      exit(); 
    } 
    while ($row = mysql_fetch_assoc($result))
    {
	    echo(json_encode($row));
    }
}
}
/* Closes Connection to MySQL server */ 
mysql_close(); 
exit();
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.