Порблем цикъл while, скриптче

Noxy

Registered
Това ми е едно скриптче...
Код:
<?php

include "currency.php";
$hedlines=$_GET['hedlines'];
// Include the news class (needed to display news)
include "Classes/news.class.php";
$Template = (isset($_GET['Template']) ? $_GET['Template'] : 'Default');
// This example includes news display with readmore functions
$Dir = NULL;

            function GetTemplateS($whattemp,$Template,$abpath,$catlist=NULL,$catlist1=NULL)
            {
            // Start Sub Function
                  $thetemp = ""; 
                        
                  // Check if the template exists            
                  // Grab the template specified
                  if (!is_dir($abpath . "Templates/" . $Template))
                  {            
                        //starting directory
                          $startDir = $abpath . "Templates";
                          //open directory
                          // Silently to error Trap - TSL
                          if ($openDir = @opendir($startDir))
                          {
                                 while($path = readdir($openDir))
                                 {
                                          //gets the base name of file i.e instead of /home/canivour/html/index.html its index.html
                                          $file = basename($path);
                                          //makes sure we dont we read the . and .. direcotry (current directory and parent directory)
                                          if($file!="." && $file!="..")
                                          { 
                                                 //if its not a directory print out the stats (can be changed for your needs)
                                                 if(is_dir($startDir."/".$file))
                                                 {
                                                        $fullDir = $startDir."/".$file; //full directory path of file
                                                        $statCheck = stat($fullDir); //keeps info on file
                                                        $Dir = $file;
                                                 }  
                                        } 
                                       }
                         }
                         // End search for new template set
                         
                        echo "<Br><font color=red><b>Error,</b> Could Not Find: \"<i>Templates/" . $Template . "/" . $whattemp . "\"</i>.<br>";
                        $Template = $Dir;
                        echo "Using: \"<i>Templates/" . $Template . "/" . $whattemp . "\"</i> Instead.</font><br>";
                              
                  }
                  $file = @fopen($abpath . "Templates/" . $Template . "/" . $whattemp, "r")
                        or die("<Br><font color=red><b>Error,</b> Failed to Open: \"<i>Templates/" . $Template . "/" . $whattemp . "\"</i>.<br>");
                  $thetemp .= @fread($file, 200000); 
                  @fclose($file);
                  
                  $CatList = $catlist;
                  
                  $thetemp = addslashes($thetemp); 
                  eval("\$thetemp = \"$thetemp\";"); 
                  $thetemp = stripslashes($thetemp); 
                  // Send back the template
                  return $thetemp;
            // End Sub Function
            }
include "Config/mysql.php";      
        $dbl = @mysql_connect($hn, $un, $pw);
        mysql_select_db($db); $chars="SET CHARACTER SET cp1251";mysql_query($chars);  


      
      // -----------------------------------------------------------------
            
            $catlist = NULL;
            $sqles2 = mysql_query("SELECT * FROM " . $tprefix . "news WHERE category ='55'");
            if ($sqles2)
            {
                  while($row = mysql_fetch_array($sqles2))
                  {
                  $price=$row[price];
                  $oldprice=$row[oldprice];
                  $currency2=$row[currency];
            
$query="SELECT `leva` FROM " . $tprefix . "finansi WHERE id ='2'";
              $result=mysql_query($query);
$euro=mysql_result($result,0,"leva");

$query="SELECT `leva` FROM " . $tprefix . "finansi WHERE id ='1'";
              $result=mysql_query($query);
$usd=mysql_result($result,0,"leva");


$currency=$_POST[currency];

$rest=$_POST[B2];

if ($rest=="Reset"){$currency="";
$currency1=$currency2;}

if ($currency==""){$price2=$price;
$currency1=$currency2;}
else{


if($currency=="EUR"){
if($currency2=="USD"){

$price2=round($price*$euro/$usd,2);
$oldprice=round($oldprice*$euro/$usd,2);
}
if($currency2=="LEV"){

$price2=round($price/$euro,2);
$oldprice=round($oldprice/$euro, 2);
}if($currency2=="EUR"){
$price2=round($price,2);
$oldprice=round($oldprice, 2);
}$currency1=$currency;}        


if($currency=="USD"){
if($currency2=="USD"){

$price2=round($price,2);
$oldprice=round($oldprice,2);
}
if($currency2=="LEV"){

$price2=round($price/$usd,2);
$oldprice=round($oldprice/$usd, 2);
}
if($currency2=="EUR"){
$price2=round($price*$usd/$euro,2);
$oldprice=round($oldprice*$usd/$euro, 2);
}$currency1=$currency;}

if($currency=="LEV"){

if($currency2=="USD"){

$price2=round($price*$usd,2);
$oldprice=round($oldprice*$usd,2);
}
if($currency2=="LEV"){

$price2=round($price,2);
$oldprice=round($oldprice, 2);
}
if($currency2=="EUR"){
$price2=round($price*$euro,2);
$oldprice=round($oldprice*$euro, 2);

}$currency1=$currency;}
}
$price2=round($price2,2);

                        $catlist .= "<option value=\"" . $price2 . "\">" . $row['news'] . "</option>\n";      
            
                  }
            }
      
            $sqles2 = mysql_query("SELECT * FROM " . $tprefix . "news WHERE category='56'");
            if ($sqles2)
            {
                  while($row = mysql_fetch_array($sqles2))
                  {
                  $price=$row[price];
                  $oldprice=$row[oldprice];
                  $currency2=$row[currency];
            
$query="SELECT `leva` FROM " . $tprefix . "finansi WHERE id ='2'";
              $result=mysql_query($query);
$euro=mysql_result($result,0,"leva");

$query="SELECT `leva` FROM " . $tprefix . "finansi WHERE id ='1'";
              $result=mysql_query($query);
$usd=mysql_result($result,0,"leva");


$currency=$_POST[currency];

$rest=$_POST[B2];

if ($rest=="Reset"){$currency="";
$currency1=$currency2;}

if ($currency==""){$price2=$price;
$currency1=$currency2;}
else{


if($currency=="EUR"){
if($currency2=="USD"){

$price2=round($price*$euro/$usd,2);
$oldprice=round($oldprice*$euro/$usd,2);
}
if($currency2=="LEV"){

$price2=round($price/$euro,2);
$oldprice=round($oldprice/$euro, 2);
}if($currency2=="EUR"){
$price2=round($price,2);
$oldprice=round($oldprice, 2);
}$currency1=$currency;}        


if($currency=="USD"){
if($currency2=="USD"){

$price2=round($price,2);
$oldprice=round($oldprice,2);
}
if($currency2=="LEV"){

$price2=round($price/$usd,2);
$oldprice=round($oldprice/$usd, 2);
}
if($currency2=="EUR"){
$price2=round($price*$usd/$euro,2);
$oldprice=round($oldprice*$usd/$euro, 2);
}$currency1=$currency;}

if($currency=="LEV"){

if($currency2=="USD"){

$price2=round($price*$usd,2);
$oldprice=round($oldprice*$usd,2);
}
if($currency2=="LEV"){

$price2=round($price,2);
$oldprice=round($oldprice, 2);
}
if($currency2=="EUR"){
$price2=round($price*$euro,2);
$oldprice=round($oldprice*$euro, 2);

}$currency1=$currency;}
}
$price2=round($price2,2);

                        $catlist1 .= "<option value=\"" . $price2 . "\">" . $row['news'] . "</option>\n";      
                  
                  }
            }

            echo GetTemplateS("configurator.tpl",$Template,$abpath,$catlist,$catlist1);

            ?>

Проблева е че този код трябва да се направи с WHILE :
Код:
            $sqles2 = mysql_query("SELECT * FROM " . $tprefix . "news WHERE category ='55'");
            if ($sqles2)
            {
                  while($row = mysql_fetch_array($sqles2))
                  {
                  $price=$row[price];
                  $oldprice=$row[oldprice];
                  $currency2=$row[currency];
            
$query="SELECT `leva` FROM " . $tprefix . "finansi WHERE id ='2'";
              $result=mysql_query($query);
$euro=mysql_result($result,0,"leva");

$query="SELECT `leva` FROM " . $tprefix . "finansi WHERE id ='1'";
              $result=mysql_query($query);
$usd=mysql_result($result,0,"leva");


$currency=$_POST[currency];

$rest=$_POST[B2];

if ($rest=="Reset"){$currency="";
$currency1=$currency2;}

if ($currency==""){$price2=$price;
$currency1=$currency2;}
else{


if($currency=="EUR"){
if($currency2=="USD"){

$price2=round($price*$euro/$usd,2);
$oldprice=round($oldprice*$euro/$usd,2);
}
if($currency2=="LEV"){

$price2=round($price/$euro,2);
$oldprice=round($oldprice/$euro, 2);
}if($currency2=="EUR"){
$price2=round($price,2);
$oldprice=round($oldprice, 2);
}$currency1=$currency;}        


if($currency=="USD"){
if($currency2=="USD"){

$price2=round($price,2);
$oldprice=round($oldprice,2);
}
if($currency2=="LEV"){

$price2=round($price/$usd,2);
$oldprice=round($oldprice/$usd, 2);
}
if($currency2=="EUR"){
$price2=round($price*$usd/$euro,2);
$oldprice=round($oldprice*$usd/$euro, 2);
}$currency1=$currency;}

if($currency=="LEV"){

if($currency2=="USD"){

$price2=round($price*$usd,2);
$oldprice=round($oldprice*$usd,2);
}
if($currency2=="LEV"){

$price2=round($price,2);
$oldprice=round($oldprice, 2);
}
if($currency2=="EUR"){
$price2=round($price*$euro,2);
$oldprice=round($oldprice*$euro, 2);

}$currency1=$currency;}
}
$price2=round($price2,2);

                        $catlist .= "<option value=\"" . $price2 . "\">" . $row['news'] . "</option>\n";      
            
                  }
            }

аз пробвах нещо ама нестана... моля за помощ...
Код:
$i=0;
while($i<60)
{$sqles2 = mysql_query("SELECT * FROM " . $tprefix . "news WHERE category ='$i'");
		if ($sqles2)
		{
			while($row = mysql_fetch_array($sqles2))
			{
			$price=$row[price];
			$oldprice=$row[oldprice];
			$currency2=$row[currency];
		
$query="SELECT `leva` FROM " . $tprefix . "finansi WHERE id ='2'";
              $result=mysql_query($query);
$euro=mysql_result($result,0,"leva");

$query="SELECT `leva` FROM " . $tprefix . "finansi WHERE id ='1'";
              $result=mysql_query($query);
$usd=mysql_result($result,0,"leva");


$currency=$_POST[currency];

$rest=$_POST[B2];

if ($rest=="Reset"){$currency="";
$currency1=$currency2;}

if ($currency==""){$price2=$price;
$currency1=$currency2;}
else{


if($currency=="EUR"){
if($currency2=="USD"){

$price2=round($price*$euro/$usd,2);
$oldprice=round($oldprice*$euro/$usd,2);
}
if($currency2=="LEV"){

$price2=round($price/$euro,2);
$oldprice=round($oldprice/$euro, 2);
}if($currency2=="EUR"){
$price2=round($price,2);
$oldprice=round($oldprice, 2);
}$currency1=$currency;}        


if($currency=="USD"){
if($currency2=="USD"){

$price2=round($price,2);
$oldprice=round($oldprice,2);
}
if($currency2=="LEV"){

$price2=round($price/$usd,2);
$oldprice=round($oldprice/$usd, 2);
}
if($currency2=="EUR"){
$price2=round($price*$usd/$euro,2);
$oldprice=round($oldprice*$usd/$euro, 2);
}$currency1=$currency;}

if($currency=="LEV"){

if($currency2=="USD"){

$price2=round($price*$usd,2);
$oldprice=round($oldprice*$usd,2);
}
if($currency2=="LEV"){

$price2=round($price,2);
$oldprice=round($oldprice, 2);
}
if($currency2=="EUR"){
$price2=round($price*$euro,2);
$oldprice=round($oldprice*$euro, 2);

}$currency1=$currency;}
}
$price2=round($price2,2);

				$catlist[$i] .= "<option value=\"" . $price2 . "\">" . $row['news'] . "</option>\n";	
		
			}
	}
echo $catlist[$i];
$i++;

}
 
малко да го опростя ...
Код:
$i=0;

while($i<60)
{$sqles2 = mysql_query("SELECT * FROM " . $tprefix . "news WHERE category ='$i'");
            if ($sqles2)
            {
                  while($row = mysql_fetch_array($sqles2))
                  {
               
         
  

                        $catlist[$i] .= "<option value=\"" . $row[price]; . "\">" . $row['news'] . "</option>\n";      
            
                  }
      }
echo $catlist[$i];
$i++;

}

Как да стане това че $catlist да получи всеки път стоиноста си и различен индекс ?
 
Браво, вече не очаквах да се сетиш, че никой няма да ти прегледа кода, без дори да знае какъв проблем да търси.

$i=0;

while($i<60)
{$sqles2 = mysql_query("SELECT * FROM " . $tprefix . "news WHERE category ='$i'");
if ($sqles2)
{
while($row = mysql_fetch_array($sqles2))
{




$catlist .= "<option value=\"" . $row[price]; . "\">" . $row['news'] . "</option>\n";

}
}
echo $catlist;
$i++;

}

така не става ли?
Между другото тия заявки в while цикъла никак не ми харесват.
 
става въпрос че трябва премерно на catlist[1] да има няколко стоиности... а то получава само една и отива на [2;]
 
Noxy каза:
става въпрос че трябва премерно на catlist[1] да има няколко стоиности... а то получава само една и отива на [2;]

Я обясни какво искаш да направиш.


така като гледам последния код който си дал, си мисля, че имаш една таблица с новини. Има котегории и ти искаш първите 60 категории да ги сложиш в един падащ списък? това ли искаш да направиш?
 
да, но не са категопиил. всяка новина примерно е в категория и искам да извадя всички новини от категория $i заедно с row[price] в падащо меню
 
Noxy каза:
да, но не са категопиил. всяка новина примерно е в категория и искам да извадя всички новини от категория $i заедно с row[price] в падащо меню

ами тогава кода в предния ми пост, би трябвало да ти върши работа и да работи, ако махнеш излишната точка и запетая след $row[price]


ето го същото, ама без единия while цикъл:

$sqles2 = mysql_query("SELECT * FROM " . $tprefix . "news WHERE category < 60");
if ($sqles2) {
while($row = mysql_fetch_array($sqles2)) {
$catlist .= "<option value="".$row[price]."">".$row['news']."</option>\n";
}
}
echo $catlist;
 
Това ми нрябваше :lol:
Код:
$i=0;

while($i<60) {
  $sqles2 = mysql_query("SELECT * FROM " . $tprefix . "news WHERE category ='$i'");
   if ($sqles2)   {
               
               while($row = mysql_fetch_array($sqles2)) {
                     $catlist2 .= "<option value=\"" . $row['price'] . "\">" . $row['subject'] . "</option>\n";      
               
             

               }
                $catlist[$i]=$catlist2;
                $catlist2="";
   }
$i++;
}

echo '<pre>';
print_r($catlist);
echo '</pre>';
 
Noxy каза:
Това ми нрябваше :lol:
Код:
$i=0;

while($i<60) {
  $sqles2 = mysql_query("SELECT * FROM " . $tprefix . "news WHERE category ='$i'");
   if ($sqles2)   {
               
               while($row = mysql_fetch_array($sqles2)) {
                     $catlist2 .= "<option value="" . $row['price'] . "">" . $row['subject'] . "</option>\n";      
               
             

               }
                $catlist[$i]=$catlist2;
                $catlist2="";
   }
$i++;
}

echo '<pre>';
print_r($catlist);
echo '</pre>';

да беше казал, че искаш данните в масив. :) Макар, че не знам защо ти трябваше да го прехвърляш от асоциативен в числов масив.
Сложи условието на първия while цикъл в заявката, както аз съм го нарпавил. Така вместо 60 заявки към базата ще имаш само една!
 

Back
Горе