0

Guys I have this query, that gets a keyword, and searches in DB for an array of articles to show according that keyword query results, then I need to fetch information about those articles according the ids I get and show it. The query is not well done, could someone help me clean the next code and improve it?

Thank you

$cleanKeyword = str_replace("+", " ", $keyword);
$i = mysql_query("SELECT IdNorma FROM $cpalabras WHERE Descripcion = '$cleanKeyword'");
  while($materia = mysql_fetch_array($i)){
       // storing idNorma into a variable
       $IdNorma = $materia['IdNorma'];
  $SQL = "SELECT n.*, j.Descripcion as jurisdiccion, 
          t.Descripcion as tipo, 
          date_format(n.FechaDesde,'%d') as dia, 
          date_format(n.FechaDesde,'%m') as mes, 
          date_format(n.FechaDesde,'%Y') as anio 
          FROM c_normas as n, c_jurisdiccion as j, c_tiponorma as t, c_materia as m
          WHERE 1=1 ";
   $SQL .= "AND n.IdNorma = '$IdNorma' ";
   $SQL .= "ORDER BY Fechadesde DESC LIMIT 300";

   var_dump($SQL);
   $a = mysql_query($SQL);
       if(mysql_num_rows($a) > 0) {
         while($row = mysql_fetch_array($a)){ ?>
           <tr style="display:inline-block;width:100%;border-bottom:1px solid #E3E6EB;padding:4px 0;font-size: 10px;" class="listaEventoUnicoTr" id="quickSearchTr">
             <td width="120" align="left" id="searchable"><strong><?php echo fromDB($row['tipo']); ?></strong></td>
             <td width="90" align="left"><a href="http://www.ips.com.ar/normmunipC/IPS_Municipal_files/files/<?php echo fromDB($row['Archivo']); ?>" target="_blank"><?php echo fromDB($row['Numero']); ?></a></td>
             <td width="90" align="left" id="searchable"><?php echo fromDB($row['dia'])."-".fromDB($row['mes'])."-".fromDB($row['anio']); ?></td>
             <td width="255" align="left" style="padding-top:4px;padding-bottom:4px;" id="searchable"><?php echo fromDB($row['Descripcion']); ?></td>
           </tr>
         <?php } //while
       } else { //if ?>
           <tr style="display:inline-block;width:100%;border-bottom:1px solid #E3E6EB;padding:4px 0;font-size: 10px;" class="listaEventoUnicoTr" id="quickSearchTr">
             <td width="500" align="center" colspan="4"> No hay resultados.</td>
           </tr>


       <?php } // else 

     }// /while ids

Thank you guys!

1
  • Start by learning how to use JOINs in SQL queries Commented Mar 3, 2011 at 13:21

2 Answers 2

2

This should help you move in the right direction:

SELECT n.*, j.descripcion as jurisdiccion, t.Descripcion as tipo, date_format(n.FechaDesde,'%d') as dia, date_format(n.FechaDesde,'%m') as mes, date_format(n.FechaDesde,'%Y') as anio

FROM c_normas as n

LEFT JOIN $cpalabras on $cpalabras.IdNorma = n.IdNorma

ORDER BY Fechadesde DESC LIMIT 300
Sign up to request clarification or add additional context in comments.

Comments

1

2 options:

  1. use JOIN to join data sources. In this case you would need only 1 query, though I'm not quite sure what it would look like, as selecting from c_normas as n, c_jurisdiccion as j, c_tiponorma as t, c_materia as m seems weird/wrong to me.
  2. after getting data from $cpalabras in first query, create list of "valid" $IdNorma values and then do a single query to select all necessary data from c_normas as n, c_jurisdiccion as j, c_tiponorma as t, c_materia as m, someting like (..) where n.IdNorma in (1, 2, 3), rather than (..) where n.IdNorma = 1.

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.