Page 1 of 1

NEWBIE question

Posted: Wed Mar 25, 2009 5:06 pm
by arrowhead
I am sorry for asking this but i need help. What i write is giving me correct result but i am very sure there is an easier and better way to write this code.

Code: Select all

 
$query_Recordset1 = "SELECT `name`,`pid` FROM list WHERE  `sanid` = 14 ORDER BY name";
$query_Recordset2 = "SELECT `name`,`pid` FROM list WHERE  `sanid` = 15 ORDER BY name";
$query_Recordset3 = "SELECT `name`,`pid` FROM list WHERE  `sanid` = 16 ORDER BY name";
$query_Recordset4 = "SELECT `name`,`pid` FROM list WHERE  `sanid` = 18 ORDER BY name";
$query_Recordset5 = "SELECT `name`,`pid` FROM list WHERE  `sanid` = 19 ORDER BY name";
 
$Recordset1 = mysql_query($query_Recordset1, $French) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
 
$Recordset2 = mysql_query($query_Recordset2, $French) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
 
$Recordset3 = mysql_query($query_Recordset3, $French) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
$totalRows_Recordset3 = mysql_num_rows($Recordset3);
 
$Recordset4 = mysql_query($query_Recordset4, $French) or die(mysql_error());
$row_Recordset4 = mysql_fetch_assoc($Recordset4);
$totalRows_Recordset4 = mysql_num_rows($Recordset4);
 
$Recordset5 = mysql_query($query_Recordset5, $French) or die(mysql_error());
$row_Recordset5 = mysql_fetch_assoc($Recordset5);
$totalRows_Recordset5 = mysql_num_rows($Recordset5);
 
 

Re: NEWBIE question

Posted: Wed Mar 25, 2009 5:13 pm
by Charles256

Code: Select all

 
$queries=array();
$results=array();
$num_records=array();
for($i=14;$i<20;$i++)
{
  $queries[] = "SELECT `name`,`pid` FROM list WHERE  `sanid` = $i ORDER BY name";
}
foreach($queries as $query)
{
  if(isset($temp)){  unset($temp);}
  $temp=mysql_query($query,$French) or die(mysql_error());
  $results[]=mysql_fetch_assoc($temp);
  $num_records[]=mysql_num_rows($temp);
}
 
that's not debugged at all and it's the same thing you did, just a bit more condensed. Hope that helps some.

Re: NEWBIE question

Posted: Wed Mar 25, 2009 5:19 pm
by arrowhead
then how i am going to insert the code below to my page. I was using this:

Code: Select all

<li style="list-style:none"><a href="sanatci.php?sid=<?php echo $row_Recordset5['sanid']; ?><?php echo $row_Recordset5['name']; ?></a>
   <ul> 
     <?php
  do {?>
<li style="list-style:none"><a href="sarki.php?sarki=<?php echo $row_Recordset5['sarkid']; ?>"><?php echo $row_Recordset5['sarkname']; ?></a></li>
   <?php } while ($row_Recordset5 = mysql_fetch_assoc($Recordset5)); ?>
      </ul>
</li>
Result looks like:

MADONNA
* Song Name
* Song Name
* Song Name

METALLICA
* Song Name
* Song Name

Re: NEWBIE question

Posted: Wed Mar 25, 2009 7:38 pm
by califdon
You need to explain what you're trying to do. I'm guessing that you only need one query, but it's not at all clear what data you have in your database or what part of it you're trying to extract.