0

Ok, so I have this function, wich I use many time in my page, but only work when I call the function one time, or if a function with results i call it first...

this is the query:

$resultClientes = mysql_query("SELECT nombre.idNombre
  , nombre.nombre AS nombreNombre
  , comuna.nombre AS nombreComuna
  , giro.nombre AS nombreGiro
  , provincia.nombre AS nombreProvincia
  , provincia.region_idRegion AS idRegion 
FROM nombre 
INNER JOIN comuna ON nombre.Comuna_idComuna = comuna.idComuna 
INNER JOIN giro ON nombre.Giro_idGiro = giro.idGiro 
INNER JOIN provincia ON comuna.Provincia_idProvincia = provincia.idProvincia 
ORDER BY nombreProvincia, nombreComuna, nombreGiro, nombreNombre");

and this is the function:

function listaClientesPorRegion($numReg,$query){
  $nombreProvincia = "";
  $nombreComuna = "";
  $nombreGiro = "";
  $nombreNombre = array();
  $i=0;

  while ($row = mysql_fetch_assoc($query)) {
    if($row['idRegion']== $numReg){
      if ($nombreProvincia == $row['nombreProvincia']) {
        if ($nombreComuna == $row['nombreComuna']) {
          if ($nombreGiro == $row['nombreGiro']) {
            $nombreNombre[] = $row['nombreNombre'];
          }
          else { //nombreGiro
            echo '<li>' . implode('</li><li>', $nombreNombre).'</li></ul></li></ul>';
        $nombreGiro = $row['nombreGiro'];
        echo '<ul class="clientes_giro"><li>'.$nombreGiro.'<ul 
                   class="clientes_nombre">';
        $nombreNombre = array($row['nombreNombre']);
          }
        }
        else { // nombreComuna
          echo '<li>' . implode('</li><li>', $nombreNombre).
               '</li></ul></li></ul></li></ul>';
          $nombreComuna = $row['nombreComuna'];
          echo '<ul class="clientes_comuna"><li>'.$nombreComuna;
          $nombreGiro = $row['nombreGiro'];
          echo '<ul class="clientes_giro"><li>'.$nombreGiro.'<ul
                class="clientes_nombre">';
          $nombreNombre = array($row['nombreNombre']);
        }
      }
      else { // nombreProvincia
        if (!empty($nombreNombre)) {
          echo '<li>' . implode('</li><li>', $nombreNombre).
               '</li></ul></li></ul></li></ul></li></ul></div>';
        }
        $class = $i++ % 2 ? 'clientes_floatEven' : 'clientes_floatOdd';

        $nombreProvincia = $row['nombreProvincia'];
        echo '<div id="'.$class.'"><ul class="clientes_provincia">
              <li><div class="underline_yellow">'.$nombreProvincia.'</div>';
        $nombreComuna = $row['nombreComuna'];
        echo '<ul class="clientes_comuna"><li>'.$nombreComuna;
        $nombreGiro = $row['nombreGiro'];
        echo '<ul class="clientes_giro"><li>'.$nombreGiro.'<ul 
        class="clientes_nombre">';
        $nombreNombre = array($row['nombreNombre']);
      }
    }
  }
  if (!empty($nombreNombre)) {
    echo '<li>' . implode('</li><li>', $nombreNombre).
         '</li></ul></li></ul></li></ul></li></ul></div>';  
 }
}

and this is part of the html

<div id="atacama"> 
  <ul class="regiones_nomb_container"> 
    <li class="atacama"></li>
  </ul>
  <?php
    listaClientesPorRegion(3,$resultClientes);
  ?>
</div>

<div id="coquimbo"> 
  <ul class="regiones_nomb_container"> 
    <li class="coquimbo"></li>
  </ul> 
  <?php
    listaClientesPorRegion(4,$resultClientes);
  ?>
</div>

<div id="valparaiso"> 
  <ul class="regiones_nomb_container"> 
    <li class="valparaiso"></li>
  </ul>
  <?php
    listaClientesPorRegion(5,$resultClientes);
  ?>
</div>

At first I tried the function with only one time and with a query that I knew would have records, like listaClientesPorRegion(9, $resultClientes), then I tried with a number I knew would not have records, both worked, so I called multiple times for all the divs in my website, but now is not working, I have no idea why... (I'm new in php and stuff), thank you for your help!

4
  • My first thought is, return an array/object from your function and display the result separately instead of echoing within the function. Or at least return a string of the result... I just don't like echo's or print's within functions/methods. Commented Apr 26, 2011 at 1:10
  • I'll probably be castigated for this suggestion, but look into Smarty. Separating presentation from your code is an important step to making your code reusable and robust (if you do it right). Commented Apr 26, 2011 at 1:14
  • There's also Flexy, which is more of a template engine, but I find the documentation tiresome. Commented Apr 26, 2011 at 1:22
  • Smarty? Really? Smarty !== separation of presentation code. Smarty is a waste of time and extra overhead/learning curve you do not need. Why learn Smarty when you can just do it in HTML? Neither Smarty nor Flexy will help with this situation. Seriously, those types of projects should be banned. They are useless. Commented Apr 26, 2011 at 2:07

1 Answer 1

1

There are a handful of issues with the code.
First, it looks like you are sending the IdRegion and the results of the query to your function as ($numReg,$query).
This means you will have to parse through every record in the results set to display what you are looking for.

Move the query into the function and only pass the idRegion.
Second, do some error checking on the $numReg to make sure something gets passed.
Third, put the $numReg value in the query to limit the results automatically.
That way you only pull the records you intend to use.

Here is the sample code:

function listaClientesPorRegion($numReg = null){
    if(!$numReg) {
        // handling here (i.e. return null, echo 'Error!';, etc.)
    }
    $nombreProvincia = "";
    $nombreComuna = "";
    $nombreGiro = "";
    $nombreNombre = array();
    $i=0;

    $resultClientes = mysql_query("SELECT nombre.idNombre
      , nombre.nombre AS nombreNombre
      , comuna.nombre AS nombreComuna
      , giro.nombre AS nombreGiro
      , provincia.nombre AS nombreProvincia
      , provincia.region_idRegion AS idRegion 
    FROM nombre 
    WHERE provincia.region_idRegion = $numReg 
    INNER JOIN comuna ON nombre.Comuna_idComuna = comuna.idComuna 
    INNER JOIN giro ON nombre.Giro_idGiro = giro.idGiro 
    INNER JOIN provincia ON comuna.Provincia_idProvincia = provincia.idProvincia 
    ORDER BY nombreProvincia, nombreComuna, nombreGiro, nombreNombre");

    while ($row = mysql_fetch_assoc($query)) {
   ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.