Jump to content


Photo

For Or While Qual Utilizar, Como ?


  • Faça o login para participar
3 replies to this topic

#1 id4689

id4689

    Turista

  • Usuários
  • 49 posts
  • Sexo:Não informado

Posted 10/11/2011, 22:35

Olá,
Gostaria de saber como simplificar esse código... alguém pode ajudar ?

<?php
session_start();
header("Content-Type: text/html; charset=ISO-8859-1",true);
include "connect.php";

if(isset($_SESSION['numero'])) 
{
//echo $_SESSION['numero'].'<br>';
$numero = $_SESSION['numero']+25;
$_SESSION['numero'] = $numero;
$numero = $_SESSION['numero'];
}
else
{
$_SESSION['numero'] = 0;
$numero = $_SESSION['numero'];
}

$soma_01 =  1+$numero;
$soma_02 =  2+$numero;
$soma_03 =  3+$numero;
$soma_04 =  4+$numero;
$soma_05 =  5+$numero;
$soma_06 =  6+$numero;
$soma_07 =  7+$numero;
$soma_08 =  8+$numero;
$soma_09 =  9+$numero;
$soma_10 = 10+$numero;
$soma_11 = 11+$numero;
$soma_12 = 12+$numero;
$soma_13 = 13+$numero;
$soma_14 = 14+$numero;
$soma_15 = 15+$numero;
$soma_16 = 16+$numero;
$soma_17 = 17+$numero;
$soma_18 = 18+$numero;
$soma_19 = 19+$numero;
$soma_20 = 20+$numero;
$soma_21 = 21+$numero;
$soma_22 = 22+$numero;
$soma_23 = 23+$numero;
$soma_24 = 24+$numero;
$soma_25 = 25+$numero;
//echo $soma_25;

$query_01 = "select xml from rss where id = '$soma_01'";
$query_02 = "select xml from rss where id = '$soma_02'";
$query_03 = "select xml from rss where id = '$soma_03'";
$query_04 = "select xml from rss where id = '$soma_04'";
$query_05 = "select xml from rss where id = '$soma_05'";
$query_06 = "select xml from rss where id = '$soma_06'";
$query_07 = "select xml from rss where id = '$soma_07'";
$query_08 = "select xml from rss where id = '$soma_08'";
$query_09 = "select xml from rss where id = '$soma_09'";
$query_10 = "select xml from rss where id = '$soma_10'";
$query_11 = "select xml from rss where id = '$soma_11'";
$query_12 = "select xml from rss where id = '$soma_12'";
$query_13 = "select xml from rss where id = '$soma_13'";
$query_14 = "select xml from rss where id = '$soma_14'";
$query_15 = "select xml from rss where id = '$soma_15'";
$query_16 = "select xml from rss where id = '$soma_16'";
$query_17 = "select xml from rss where id = '$soma_17'";
$query_18 = "select xml from rss where id = '$soma_18'";
$query_19 = "select xml from rss where id = '$soma_19'";
$query_20 = "select xml from rss where id = '$soma_20'";
$query_21 = "select xml from rss where id = '$soma_21'";
$query_22 = "select xml from rss where id = '$soma_22'";
$query_23 = "select xml from rss where id = '$soma_23'";
$query_24 = "select xml from rss where id = '$soma_24'";
$query_25 = "select xml from rss where id = '$soma_25'";


$_sql = "SELECT * FROM rss";
$_res = mysql_query($_sql,$_con);
$_nr = mysql_num_rows($_res);
//echo $_nr;
//echo $numero;
//echo $soma_25;

if(
($soma_01 <= $_nr) xor 
($soma_02 <= $_nr) xor 
($soma_03 <= $_nr) xor 
($soma_04 <= $_nr) xor 
($soma_05 <= $_nr) xor    
($soma_06 <= $_nr) xor
($soma_07 <= $_nr) xor 
($soma_08 <= $_nr) xor 
($soma_09 <= $_nr) xor 
($soma_10 <= $_nr) xor 
($soma_11 <= $_nr) xor    
($soma_12 <= $_nr) xor
($soma_13 <= $_nr) xor 
($soma_14 <= $_nr) xor 
($soma_15 <= $_nr) xor 
($soma_16 <= $_nr) xor 
($soma_17 <= $_nr) xor    
($soma_18 <= $_nr) xor
($soma_19 <= $_nr) xor 
($soma_20 <= $_nr) xor 
($soma_21 <= $_nr) xor    
($soma_22 <= $_nr) xor
($soma_23 <= $_nr) xor 
($soma_24 <= $_nr) xor 
($soma_25 <= $_nr)) {

 

echo 'codigo';


$query_01 = mysql_query($query_01,$_con);
$y = mysql_fetch_array($query_01);
$_res = $y["xml"];
echo $_res;
if($soma_01 < $_nr){
echo ",";
}

$query_02 = mysql_query($query_02,$_con);
$y = mysql_fetch_array($query_02);
$_res = $y["xml"];
echo $_res;
if($soma_02 < $_nr){
echo ",";
}

$query_03 = mysql_query($query_03,$_con);
$y = mysql_fetch_array($query_03);
$_res = $y["xml"];
echo $_res;
if($soma_03 < $_nr){
echo ",";
}

$query_04 = mysql_query($query_04,$_con);
$y = mysql_fetch_array($query_04);
$_res = $y["xml"];
echo $_res;
if($soma_04 < $_nr){
echo ",";
}

$query_05 = mysql_query($query_05,$_con);
$y = mysql_fetch_array($query_05);
$_res = $y["xml"];
echo $_res;
if($soma_05 < $_nr){
echo ",";
}

$query_06 = mysql_query($query_06,$_con);
$y = mysql_fetch_array($query_06);
$_res = $y["xml"];
echo $_res;
if($soma_06 < $_nr){
echo ",";
}

$query_07 = mysql_query($query_07,$_con);
$y = mysql_fetch_array($query_07);
$_res = $y["xml"];
echo $_res;
if($soma_07 < $_nr){
echo ",";
}

$query_08 = mysql_query($query_08,$_con);
$y = mysql_fetch_array($query_08);
$_res = $y["xml"];
echo $_res;
if($soma_08 < $_nr){
echo ",";
}

$query_09 = mysql_query($query_09,$_con);
$y = mysql_fetch_array($query_09);
$_res = $y["xml"];
echo $_res;
if($soma_09 < $_nr){
echo ",";
}

$query_10 = mysql_query($query_10,$_con);
$y = mysql_fetch_array($query_10);
$_res = $y["xml"];
echo $_res;
if($soma_10 < $_nr){
echo ",";
}

$query_11 = mysql_query($query_11,$_con);
$y = mysql_fetch_array($query_11);
$_res = $y["xml"];
echo $_res;
if($soma_11 < $_nr){
echo ",";
}

$query_12 = mysql_query($query_12,$_con);
$y = mysql_fetch_array($query_12);
$_res = $y["xml"];
echo $_res;
if($soma_12 < $_nr){
echo ",";
}

$query_13 = mysql_query($query_13,$_con);
$y = mysql_fetch_array($query_13);
$_res = $y["xml"];
echo $_res;
if($soma_13 < $_nr){
echo ",";
}

$query_14 = mysql_query($query_14,$_con);
$y = mysql_fetch_array($query_14);
$_res = $y["xml"];
echo $_res;
if($soma_14 < $_nr){
echo ",";
}

$query_15 = mysql_query($query_15,$_con);
$y = mysql_fetch_array($query_15);
$_res = $y["xml"];
echo $_res;
if($soma_15 < $_nr){
echo ",";
}

$query_16 = mysql_query($query_16,$_con);
$y = mysql_fetch_array($query_16);
$_res = $y["xml"];
echo $_res;
if($soma_16 < $_nr){
echo ",";
}

$query_17 = mysql_query($query_17,$_con);
$y = mysql_fetch_array($query_17);
$_res = $y["xml"];
echo $_res;
if($soma_17 < $_nr){
echo ",";
}

$query_18 = mysql_query($query_18,$_con);
$y = mysql_fetch_array($query_18);
$_res = $y["xml"];
echo $_res;
if($soma_18 < $_nr){
echo ",";
}

$query_19 = mysql_query($query_19,$_con);
$y = mysql_fetch_array($query_19);
$_res = $y["xml"];
echo $_res;
if($soma_19 < $_nr){
echo ",";
}

$query_20 = mysql_query($query_20,$_con);
$y = mysql_fetch_array($query_20);
$_res = $y["xml"];
echo $_res;
if($soma_20 < $_nr){
echo ",";
}

$query_21 = mysql_query($query_21,$_con);
$y = mysql_fetch_array($query_21);
$_res = $y["xml"];
echo $_res;
if($soma_21 < $_nr){
echo ",";
}

$query_22 = mysql_query($query_22,$_con);
$y = mysql_fetch_array($query_22);
$_res = $y["xml"];
echo $_res;
if($soma_22 < $_nr){
echo ",";
}

$query_23 = mysql_query($query_23,$_con);
$y = mysql_fetch_array($query_23);
$_res = $y["xml"];
echo $_res;
if($soma_23 < $_nr){
echo ",";
}

$query_24 = mysql_query($query_24,$_con);
$y = mysql_fetch_array($query_24);
$_res = $y["xml"];
echo $_res;
if($soma_24 < $_nr){
echo ",";
}


$query_25 = mysql_query($query_25,$_con);
$y = mysql_fetch_array($query_25);
$_res = $y["xml"];
echo $_res;

echo "]}}";

}
// PARA PARAR O REFLESH
else
{

echo  'codigo_stop';



$query_01 = mysql_query($query_01,$_con);
$y = mysql_fetch_array($query_01);
$_res = $y["xml"];
echo $_res;
if($soma_01 < $_nr){
echo ",";
}

$query_02 = mysql_query($query_02,$_con);
$y = mysql_fetch_array($query_02);
$_res = $y["xml"];
echo $_res;
if($soma_02 < $_nr){
echo ",";
}

$query_03 = mysql_query($query_03,$_con);
$y = mysql_fetch_array($query_03);
$_res = $y["xml"];
echo $_res;
if($soma_03 < $_nr){
echo ",";
}

$query_04 = mysql_query($query_04,$_con);
$y = mysql_fetch_array($query_04);
$_res = $y["xml"];
echo $_res;
if($soma_04 < $_nr){
echo ",";
}

$query_05 = mysql_query($query_05,$_con);
$y = mysql_fetch_array($query_05);
$_res = $y["xml"];
echo $_res;
if($soma_05 < $_nr){
echo ",";
}

$query_06 = mysql_query($query_06,$_con);
$y = mysql_fetch_array($query_06);
$_res = $y["xml"];
echo $_res;
if($soma_06 < $_nr){
echo ",";
}

$query_07 = mysql_query($query_07,$_con);
$y = mysql_fetch_array($query_07);
$_res = $y["xml"];
echo $_res;
if($soma_07 < $_nr){
echo ",";
}

$query_08 = mysql_query($query_08,$_con);
$y = mysql_fetch_array($query_08);
$_res = $y["xml"];
echo $_res;
if($soma_08 < $_nr){
echo ",";
}

$query_09 = mysql_query($query_09,$_con);
$y = mysql_fetch_array($query_09);
$_res = $y["xml"];
echo $_res;
if($soma_09 < $_nr){
echo ",";
}

$query_10 = mysql_query($query_10,$_con);
$y = mysql_fetch_array($query_10);
$_res = $y["xml"];
echo $_res;
if($soma_10 < $_nr){
echo ",";
}

$query_11 = mysql_query($query_11,$_con);
$y = mysql_fetch_array($query_11);
$_res = $y["xml"];
echo $_res;
if($soma_11 < $_nr){
echo ",";
}

$query_12 = mysql_query($query_12,$_con);
$y = mysql_fetch_array($query_12);
$_res = $y["xml"];
echo $_res;
if($soma_12 < $_nr){
echo ",";
}

$query_13 = mysql_query($query_13,$_con);
$y = mysql_fetch_array($query_13);
$_res = $y["xml"];
echo $_res;
if($soma_13 < $_nr){
echo ",";
}

$query_14 = mysql_query($query_14,$_con);
$y = mysql_fetch_array($query_14);
$_res = $y["xml"];
echo $_res;
if($soma_14 < $_nr){
echo ",";
}

$query_15 = mysql_query($query_15,$_con);
$y = mysql_fetch_array($query_15);
$_res = $y["xml"];
echo $_res;
if($soma_15 < $_nr){
echo ",";
}

$query_16 = mysql_query($query_16,$_con);
$y = mysql_fetch_array($query_16);
$_res = $y["xml"];
echo $_res;
if($soma_16 < $_nr){
echo ",";
}

$query_17 = mysql_query($query_17,$_con);
$y = mysql_fetch_array($query_17);
$_res = $y["xml"];
echo $_res;
if($soma_17 < $_nr){
echo ",";
}

$query_18 = mysql_query($query_18,$_con);
$y = mysql_fetch_array($query_18);
$_res = $y["xml"];
echo $_res;
if($soma_18 < $_nr){
echo ",";
}

$query_19 = mysql_query($query_19,$_con);
$y = mysql_fetch_array($query_19);
$_res = $y["xml"];
echo $_res;
if($soma_19 < $_nr){
echo ",";
}

$query_20 = mysql_query($query_20,$_con);
$y = mysql_fetch_array($query_20);
$_res = $y["xml"];
echo $_res;
if($soma_20 < $_nr){
echo ",";
}

$query_21 = mysql_query($query_21,$_con);
$y = mysql_fetch_array($query_21);
$_res = $y["xml"];
echo $_res;
if($soma_21 < $_nr){
echo ",";
}

$query_22 = mysql_query($query_22,$_con);
$y = mysql_fetch_array($query_22);
$_res = $y["xml"];
echo $_res;
if($soma_22 < $_nr){
echo ",";
}

$query_23 = mysql_query($query_23,$_con);
$y = mysql_fetch_array($query_23);
$_res = $y["xml"];
echo $_res;
if($soma_23 < $_nr){
echo ",";
}

$query_24 = mysql_query($query_24,$_con);
$y = mysql_fetch_array($query_24);
$_res = $y["xml"];
echo $_res;
if($soma_24 < $_nr){
echo ",";
}


$query_25 = mysql_query($query_25,$_con);
$y = mysql_fetch_array($query_25);
$_res = $y["xml"];
echo $_res;

echo "fim";

}

mysql_close($_con);

?>

Thank you :D

#2 Satoru

Satoru

    Novato no fórum

  • Usuários
  • 24 posts
  • Sexo:Masculino
  • Localidade:Minas Gerais
  • Interesses:PHP, Mysql, Javascript,Asp, HTLM, CSS e AS3

Posted 11/11/2011, 09:46

Para começar, voce poderia criar um array para substituir as variáveis $soma_
Exemplo:
//declaração do vetor soma:
$soma = array(); 

//for para atribuir os valores em cada linha do vetor:
for($i=0;$i<25;$i++){
  $soma[$i] = $i + $numero;
}

Assim ao invés de chamar por $soma_01 voce chamará por $soma[0], $soma_02 será $soma[1], $soma_03 será $soma[2] ... e $soma_25 será $soma[24]
É importante notar que o vetor começa de ZERO, então se são 25 linhas, o vetor começará em 0 e terminará em 24.


//Você pode fazer a mesma coisa com a variável $query:

$query = array(); 

for($i=0;$i<25;$i++){
  $query[$i] = "select xml from rss where id = '$soma[$i]'";
}



//A última parte você poderia fazer assim:
for($i=0;$i<25;$i++){
$query[$i] = mysql_query($query[$i],$_con);
$y = mysql_fetch_array($query[$i]);
$_res = $y["xml"];
echo $_res;

//verifica se não é o último, e se for escreve "fim"
if($i!=24){
  if($soma[$i] < $_nr){
    echo ",";
  }
}
else{
  echo "fim";
}

}

Observação: Fiz apenas algumas partes do seu código, pois estou meio sem tempo aqui, mas já dá pra você ter uma idéia do resto.
Caso tenha alguma dúvida posta aí que respondo depois.

Edição feita por: Satoru, 11/11/2011, 09:51.


#3 Diego Bezerra

Diego Bezerra

    Diego Bezerra

  • Moderadores
  • 938 posts
  • Sexo:Masculino
  • Localidade:Amazonas
  • Interesses:Iniciando no C# com MSSQL.

Posted 15/11/2011, 15:20

Ola é por ai como nosso amigo satoru falou?
Att.
Diego Bezerra
Agência Kernel - em Breve
Email/msn: diegobezerra@outlook.com
Facebook

#4 id4689

id4689

    Turista

  • Usuários
  • 49 posts
  • Sexo:Não informado

Posted 16/11/2011, 03:03

Ola é por ai como nosso amigo satoru falou?


sim é bem por ai cara, tenho dificuldades nisso
agora olhando pelo exemplo deu uma noção...

//declaração do vetor soma:
$soma = array(); 

//for para atribuir os valores em cada linha do vetor:
for($i=0;$i<25;$i++){
  $soma[$i] = $i + $numero;
}

//Você pode fazer a mesma coisa com a variável $query:

$query = array(); 

for($i=0;$i<25;$i++){
  $query[$i] = "select xml from rss where id = '$soma[$i]'";
}



//A última parte você poderia fazer assim:
for($i=0;$i<25;$i++){
$query[$i] = mysql_query($query[$i],$_con);
$y = mysql_fetch_array($query[$i]);
$_res = $y["xml"];
echo $_res;

//verifica se não é o último, e se for escreve "fim"
if($i!=24){
  if($soma[$i] < $_nr){
    echo ",";
  }
}
else{
  echo "fim";
}

}


uma boa noção ainda... ae Satoru poxa já deu para entender legal já,
o resto eu me viro aqui kkk já simplificou td praticamente.
obrigado




1 user(s) are reading this topic

0 membro(s), 1 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq