Jump to content

Help how to draft correctly a php snippet


hookit

Recommended Posts

Hi,

I have been given this php snippet:

 

$Res = mysql_query($chainSQL);

if(mysql_num_rows($Res)>0){

#If results found show list

while($otmp = mysql_fetch_object($Res)){}

}else{

echo 'NO RESULTS FOUND;

}

 

In this script i must introduce the dynamic´s table code in order to get the echo output if the condition is met. Here is where i been told to write the table´s code:

 

if(mysql_num_rows($Res)>0){

//Here introduce tablè's code

}

 

Well, my doubt comes to how embed this code into because i wrote it as this but the code remains in black text not changing into colors when the php setence is right:

 

<table border="1" align="center" bordercolor="#88D9FF" bgcolor="#D0EEFB">

    <tr background="images/logofondoregion.gif">

      <td class="style19"><div align="center" class="style20">Población</div></td>

      <td class="style19"><div align="center" class="style20">Dirección</div></td>

      <td class="style19"><div align="center" class="style20">Día</div></td>

      <td class="style19"><div align="center" class="style20">Hora</div></td>

      <td class="style19"><div align="center" class="style20">Formato</div></td>

      <td class="style19"><div align="center" class="style20">Observaciones</div></td>

    </tr>

<?php $contador = 0; //inicializa el contador fuera del loop ?>

    <?php do { ?>

      <tr <?php if ($contador++ % 2) {echo 'class="color"';}?>>

        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Población']; ?></div></td>

        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Dirección']; ?></div></td>

        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Día']; ?></div></td>

        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Hora']; ?></div></td>

        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Formato']; ?></div></td>

        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Observaciones']; ?></div></td>

      </tr>

      <?php } while ($row_reuniones = mysql_fetch_assoc($reuniones)); ?>

  </table>

 

Thanks

Link to comment
Share on other sites

//for error reporting, if no errors delete this...
ini_set ("display_errors", "1");
error_reporting(E_ALL);

$Res = mysql_query($chainSQL);
if(mysql_num_rows($Res)>0){
</pre>
<table border="1" align="center" bordercolor="#88D9FF" bgcolor="#D0EEFB">
    
      Población
      Dirección
      Día
      Hora
      Formato
      Observaciones
    
       while($otmp = mysql_fetch_object($Res)){ ?>
      >
        
        
        
        
        
        
      
      
  </table>
<br>  	echo 'NO RESULTS FOUND';<br>}

Link to comment
Share on other sites

Thanks Maq:

 

if i apply your script the server reports this error log:

 

Parse error: syntax error, unexpected '<' in /home/whatever/domains/whatever.com/public_html/results.php on line 45

 

[<table border="1" align="center" bordercolor="#88D9FF" bgcolor="#D0EEFB">/code]

It seems that there is something wrong with '<'

Thanks

Link to comment
Share on other sites

<?php 
//for error reporting, if no errors delete this...
ini_set ("display_errors", "1");
error_reporting(E_ALL);

$Res = mysql_query($chainSQL);
if(mysql_num_rows($Res)>0){ ?>
<table border="1" align="center" bordercolor="#88D9FF" bgcolor="#D0EEFB">
    <tr background="images/logofondoregion.gif">
      <td class="style19"><div align="center" class="style20">Población</div></td>
      <td class="style19"><div align="center" class="style20">Dirección</div></td>
      <td class="style19"><div align="center" class="style20">Día</div></td>
      <td class="style19"><div align="center" class="style20">Hora</div></td>
      <td class="style19"><div align="center" class="style20">Formato</div></td>
      <td class="style19"><div align="center" class="style20">Observaciones</div></td>
    </tr>
   <?php $contador = 0; //inicializa el contador fuera del loop
    while($otmp = mysql_fetch_object($Res)){ ?>
      <tr <?php if ($contador++ % 2) {echo 'class="color"';}?>>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Población']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Dirección']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Día']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Hora']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Formato']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Observaciones']; ?></div></td>
      </tr>
      <?php } ?>
  </table>
  <?php } else{
   echo 'NO RESULTS FOUND';
} ?>

Link to comment
Share on other sites

If i paste the last code posted by wayne remains with same error as before but in the first line of this snippet:

 

<?php 
//for error reporting, if no errors delete this...
ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

I can´t make it work.

Link to comment
Share on other sites

Error log:

 

Parse error: syntax error, unexpected '<' in /****/a9807048/public_html/resultados.php on line 38

 

This is qhere line 38 begins:

 

<?php 
//for error reporting, if no errors delete this...
ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

His is the whole script:

 

<?php 
//for error reporting, if no errors delete this...
ini_set ("display_errors", "1");
error_reporting(E_ALL);

$Res = mysql_query($chainSQL);
if(mysql_num_rows($Res)>0){ ?>
<table border="1" align="center" bordercolor="#88D9FF" bgcolor="#D0EEFB">
    <tr background="images/logofondoregion.gif">
      <td class="style19"><div align="center" class="style20">Población</div></td>
      <td class="style19"><div align="center" class="style20">Dirección</div></td>
      <td class="style19"><div align="center" class="style20">Día</div></td>
      <td class="style19"><div align="center" class="style20">Hora</div></td>
      <td class="style19"><div align="center" class="style20">Formato</div></td>
      <td class="style19"><div align="center" class="style20">Observaciones</div></td>
    </tr>
   <?php $contador = 0; //inicializa el contador fuera del loop
    while($otmp = mysql_fetch_object($Res)){ ?>
      <tr <?php if ($contador++ % 2) {echo 'class="color"';}?>>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Población']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Dirección']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Día']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Hora']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Formato']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Observaciones']; ?></div></td>
      </tr>
      <?php } ?>
  </table>
  <?php } else{
   echo 'NO RESULTS FOUND';
} ?>

 

Link to comment
Share on other sites

This is the code of the whole page:

 

<?php require_once('../Connections/miconexion.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_miconexion, $miconexion);
$query_reuniones = "SELECT reuniones.Población, reuniones.Dirección, reuniones.Día, reuniones.Hora, reuniones.Formato, reuniones.Observaciones FROM reuniones WHERE reuniones.Población LIKE '%".trim($_POST['textfield'])."%'";
$reuniones = mysql_query($query_reuniones, $miconexion) or die(mysql_error());
$row_reuniones = mysql_fetch_assoc($reuniones);
$totalRows_reuniones = mysql_num_rows($reuniones);
Code: [select]<?php 
//for error reporting, if no errors delete this...
ini_set ("display_errors", "1");
error_reporting(E_ALL);
$Res = mysql_query($query_reuniones);
if(mysql_num_rows($Res)>0){ ?>
<table border="1" align="center" bordercolor="#88D9FF" bgcolor="#D0EEFB">
    <tr background="images/logofondoregion.gif">
      <td class="style19"><div align="center" class="style20">Población</div></td>
      <td class="style19"><div align="center" class="style20">Dirección</div></td>
      <td class="style19"><div align="center" class="style20">Día</div></td>
      <td class="style19"><div align="center" class="style20">Hora</div></td>
      <td class="style19"><div align="center" class="style20">Formato</div></td>
      <td class="style19"><div align="center" class="style20">Observaciones</div></td>
    </tr>
   <?php $contador = 0; //inicializa el contador fuera del loop
    while($otmp = mysql_fetch_object($Res)){ ?>
      <tr <?php if ($contador++ % 2) {echo 'class="color"';}?>>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Población']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Dirección']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Día']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Hora']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Formato']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $otmp['Observaciones']; ?></div></td>
      </tr>
      <?php } ?>
  </table>
  <?php } else{
   echo 'NO RESULTS FOUND';
} ?>
?><!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" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>resultado de reuniones</title>
<!-- TemplateEndEditable -->
<style type="text/css">
<!--
.color {
background-color: #88D9FF;
}
.style3 {color: #0000FF; font-family: "Courier New", Courier, monospace; }
.style5 {
color: #0000FF;
font-family: Georgia, "Times New Roman", Times, serif;
font-weight: bold;
font-size: 14px;
}
body {
background-image: url(../logofondoregion.gif);
}
.style6 {font-size: 24px}
.style13 {
font-size: 14;
font-family: Georgia, "Times New Roman", Times, serif;
}
.style14 {
font-size: 16;
font-family: Georgia, "Times New Roman", Times, serif;
}
.style16 {color: #0000FF}
.style18 {font-family: Georgia, "Times New Roman", Times, serif}
.style19 {background-color: #88D9FF; font-family: Georgia, "Times New Roman", Times, serif; }
.style20 {font-weight: bold; color: #800040;}
.style24 {
color: #0000FF;
font-size: 16px;
font-weight: bold;
}
.style25 {font-size: 16px}
-->
</style>
<!-- TemplateBeginEditable name="head" --><!-- TemplateEndEditable -->
</head>

<body>
<div align="center">
  <h1 align="center" class="style3 style6 style18">Reuniones en <?php echo $_POST['textfield']; ?></h1>
  <p align="center" class="style5 style14 style25">Se encontraron <span class="style16"><?php echo $totalRows_reuniones ?></span> reuniones. </p>
  <p align="center" class="style5"> </p>
  <table border="1" bordercolor="#88D9FF" bgcolor="#D0EEFB">
    <tr background="../logofondoregion.gif">
      <td class="style19"><div align="center" class="style20">Población</div></td>
      <td class="style19"><div align="center" class="style20">Dirección</div></td>
      <td class="style19"><div align="center" class="style20">Día</div></td>
      <td class="style19"><div align="center" class="style20">Hora</div></td>
      <td class="style19"><div align="center" class="style20">Formato</div></td>
      <td class="style19"><div align="center" class="style20">Observaciones</div></td>
    </tr>
<?php $contador = 0; //inicializa el contador fuera del loop ?>
    <?php do { ?>
      <tr <?php if ($contador++ % 2) {echo 'class="color"';}?>>
        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Población']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Dirección']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Día']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Hora']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Formato']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Observaciones']; ?></div></td>
      </tr>
      <?php } while ($row_reuniones = mysql_fetch_assoc($reuniones)); ?>
  </table>
  <p align="center"><span class="style18"><a href="../index.php"><span class="style24">Nueva búsqueda</span></a></span></p>
</div>
<a href="../index.php">	</a>
</body>
</html>
<?php
mysql_free_result($reuniones);
?>

 

Error log and line as before.

What i want is if someone types in the textfield a registry that is not in the "Población" field of DB, instead the Dynamic table, a "NON RESULTS FOUND" would appear.

 

Thanks

 

Link to comment
Share on other sites

The code im pasting here is working properly:

<?php require_once('Connections/miconexion.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_miconexion, $miconexion);
$query_reuniones = "SELECT reuniones.Población, reuniones.Dirección, reuniones.Día, reuniones.Hora, reuniones.Formato, reuniones.Observaciones FROM reuniones WHERE reuniones.Población LIKE '%".trim($_POST['textfield'])."%'";
$reuniones = mysql_query($query_reuniones, $miconexion) or die(mysql_error());
$row_reuniones = mysql_fetch_assoc($reuniones);
$totalRows_reuniones = mysql_num_rows($reuniones);
?><!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" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>resultado de reuniones</title>
<!-- TemplateEndEditable -->
<style type="text/css">
<!--
.color {
background-color: #88D9FF;
}
.style3 {color: #0000FF; font-family: "Courier New", Courier, monospace; }
.style5 {
color: #0000FF;
font-family: Georgia, "Times New Roman", Times, serif;
font-weight: bold;
font-size: 14px;
}
body {
background-image: url(logofondoregion.gif);
}
.style6 {font-size: 24px}
.style13 {
font-size: 14;
font-family: Georgia, "Times New Roman", Times, serif;
}
.style14 {
font-size: 16;
font-family: Georgia, "Times New Roman", Times, serif;
}
.style16 {color: #0000FF}
.style18 {font-family: Georgia, "Times New Roman", Times, serif}
.style19 {background-color: #88D9FF; font-family: Georgia, "Times New Roman", Times, serif; }
.style20 {font-weight: bold; color: #800040;}
.style24 {
color: #0000FF;
font-size: 16px;
font-weight: bold;
}
.style25 {font-size: 16px}
-->
</style>
<!-- TemplateBeginEditable name="head" --><!-- TemplateEndEditable -->
</head>

<body>
<div align="center">
  <h1 align="center" class="style3 style6 style18">Reuniones en <?php echo $_POST['textfield']; ?></h1>
  <p align="center" class="style5 style14 style25">Se encontraron <span class="style16"><?php echo $totalRows_reuniones ?></span> reuniones. </p>
  <p align="center" class="style5"> </p>
  <table border="1" bordercolor="#88D9FF" bgcolor="#D0EEFB">
    <tr background="logofondoregion.gif">
      <td class="style19"><div align="center" class="style20">Población</div></td>
      <td class="style19"><div align="center" class="style20">Dirección</div></td>
      <td class="style19"><div align="center" class="style20">Día</div></td>
      <td class="style19"><div align="center" class="style20">Hora</div></td>
      <td class="style19"><div align="center" class="style20">Formato</div></td>
      <td class="style19"><div align="center" class="style20">Observaciones</div></td>
    </tr>
<?php $contador = 0; //inicializa el contador fuera del loop ?>
    <?php do { ?>
      <tr <?php if ($contador++ % 2) {echo 'class="color"';}?>>
        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Población']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Dirección']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Día']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Hora']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Formato']; ?></div></td>
        <td class="style18"><div align="center" class="style20"><?php echo $row_reuniones['Observaciones']; ?></div></td>
      </tr>
      <?php } while ($row_reuniones = mysql_fetch_assoc($reuniones)); ?>
  </table>
  <p align="center"><span class="style18"><a href="index.php"><span class="style24">Nueva búsqueda</span></a></span></p>
</div>
<a href="index.php">	</a>
</body>
</html>
<?php
mysql_free_result($reuniones);
?>

 

What i want to implement is that went a user type in the textfield's webpage any word that is not in the database instead empty the dynamic table should appear a message saying something like "NO RESULTS WERE FOUND".

And I couldn´t achieve it so far.

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.