Kuba
Kuba

Reputation: 189

Mysql query result ouput outside of while {}

I have a problem to get my sql query result fully working outside the while loop.

I have a query like this:

$result_artikel = mysql_query("SELECT DISTINCT oxtitle, FROM oxarticles a
INNER JOIN oxobject2category b ON a.oxid = b.oxobjectid
WHERE b.oxcatnid IN (SELECT oxcatnid FROM oxobject2category WHERE oxobjectid = '$term')ORDER BY oxtitle ASC")
or die(mysql_error());  ;

this gives back some names (oxtitle). For example 5:

Car, Smartphone, Chair, Table, Drink

in the next step I put this query results in an array and output them in the while loop

$Daten_artikel = array(
    'name' => '',
    'oxsort' => '',
    'bild' => ''
);

while($row = mysql_fetch_array($result_artikel))
{
    $Daten_artikel['name'] = $row['oxtitle'];
    $Daten_artikel['oxsort'] = $row['oxsort'];
    $Daten_artikel['bild'] = $row['oxpic1'];

    echo '<br>' . "Name: ".$Daten_artikel['name'];
}

The reason why I put the results in an array is that I can output them outside the while loop. This works so far, BUT when I output the result outside the while loop I have only one name (oxtitle) in the array. And this is my problem that I cant get fix.

I need the results outside the while loop cause I have later in the page a html table, that musst be filled by IF statement.

Exmaple:

<table>
<tr>
<td>
<?php
if ($Daten_artikel['name'] == smartphone )
    {
    echo $Daten_artikel['name'];
    }
?>
</td>
</tr>

<tr>
<td>
<?php
if ($Daten_artikel['name'] == chair)
    {
    echo $Daten_artikel['name'];
    }
?>
</td>
<tr>
</table>

This is just an exmaple. But this cant works, cause in the array are just one name (oxtitle) for example "car". So the if-statement cant work. It just works for: if array == car.

One soulution can be to put all my table html code with the if loops in the while loop. But this means that I musst work always inside the wile loop with echo '' and so on. And this not not so fine. I hope there is another soultion that I can it get work "my" way. Means that the array contains all name (oxtitles) even outside the while loop, so that I can work with if-statements.

Sorry for my bad english. Greetz.

Upvotes: 1

Views: 594

Answers (1)

echo_Me
echo_Me

Reputation: 37233

why dont you use this

   $row = mysql_fetch_array($result_artikel) ;

  $Daten_artikel['name'] = $row['oxtitle'];
  $Daten_artikel['oxsort'] = $row['oxsort'];
  $Daten_artikel['bild'] = $row['oxpic1'];

 // echo '<br>' . "Name: ".$Daten_artikel['name'];

instead of while loop. and then you can use those variables.

EDIT:

 <?php
   echo "<table>";
   while($row = mysql_fetch_array($result_artikel))
{
$Daten_artikel['name'] = $row['oxtitle'];
$Daten_artikel['oxsort'] = $row['oxsort'];
$Daten_artikel['bild'] = $row['oxpic1'];

//echo '<br>' . "Name: ".$Daten_artikel['name'];
?>
<tr>
<td>
<?php
if ($Daten_artikel['name'] == "smartphone" )
{
echo $Daten_artikel['name'];
}
if ($Daten_artikel['name'] == "chair")
{
echo $Daten_artikel['name'];
}
else {echo "there is nothing";} 
?>
</td>
</tr>

<?php
}
echo "</table>";
?>

EDIT2 ; im not sure i understood you , but this soluion ,

change your order by to this:

   ORDER BY case when oxtitle = 'Schwarz' then 1
                 when oxtitle = 'Cyan'    then 2
                 when oxtitle = 'Magenta'    then 3
                 when oxtitle = 'Gelb'    then 4 end 

and try this code:

    echo "<table>";
   while($row = mysql_fetch_array($result_artikel))
 {
  $Daten_artikel['name'] = $row['oxtitle'];
  $Daten_artikel['oxsort'] = $row['oxsort'];
  $Daten_artikel['bild'] = $row['oxpic1'];

  // echo '<br>' . "Name: ".$Daten_artikel['name'];
 ?>
  <tr>
  <td>xzy</td>
   <td>
     <?php
   echo $Daten_artikel['name'] ;

   ?>
   </td>
  </tr>

 <?php
   }
  echo "</table>";
  ?>

EDIT3.

  ORDER BY case when oxtitle like '%Schwarz%' then 1
             when oxtitle LIKE '%Cyan%'    then 2
             when oxtitle LIKE '%Magenta%'    then 3
             when oxtitle LIKE '%Gelb%'    then 4 end 

EDIT4: due to your needs use this

     case when oxtitle like '%Schwarz%' and (oxsort = 13 or oxsort = 3 ) then 1
     .....

Upvotes: 1

Related Questions