Jump to content

Recommended Posts

I've a php "program" that helps me manage sales.
The program organizes sales by amount and by customer.
One of the listings that the program provides is who has placed orders in the last 720 days.
However the program has a bugg that I can not identify. It orders the customers / sales but the order does not start at 01 up to 720, but rather at 010 .... 02, 021,022 .... 03, 031, 034
I looked and looked and I can not figure out where the problem is. Could you help me?

php file in pdf: https://we.tl/t-D4xTtG3Ync
Thanks

Link to comment
https://forums.phpfreaks.com/topic/308404-help-with-list-ordering/
Share on other sites

Either your date is in an unsortable format (ie anything other than yyyy-mm-dd) or you are sorting on a non numeric field so you get an alphabetic rather than numeric order.

Post code, my mother told be not to accept links from strangers,

You might get a better response if you post the pertinent code here. Just make sure to use code tags (which is the <> button in toolbar when posting).

Have you looked into using something like PHP's natsort() function? More information can be found here:
http://php.net/manual/en/function.natsort.php

Edited by cyberRobot
fixed typo
<?php
session_start();  
						  if(isset($_SESSION['Nome']))
						  $_SESSION['Nome'] = "000";
						  else
						  $_SESSION['Nome'] = "No ID"; 
						  //o título h1, h2 ou equivalente
						  $designacao="Lista de faltas";
						  //este ficheiro
						  $ficheiro="bo_2_colab_vendas_1_720_dias";
						  //activação de filtro
						  $ficheiro_filtro_activo=0;
						  //ficheiro de filtragem caso exista
						  $ficheiro_filtro=$ficheiro."_filtro";
						  //o nome da var de sessão que possui a query SQL
						  $sessao_filtro=$ficheiro_filtro;
						  //ficheiro alvo do post relativo à option presente no grid
						  $ficheiro_alvo_post_get="";
						  //ficheiro para criação de registo
						  $ficheiro_criar="";
						  //ficheiro para edição de registo						  
						  $ficheiro_editar="";
						  //ficheiro para eliminação de registo						  
						  $ficheiro_eliminar="";
						  //ficheiro para acesso à área privada do registo							  
						  $ficheiro_area_privada="";
						  $texto_legenda_area_privada="";
						  $tooltip_legenda_area_privada="";
						  //qt de colunas mas não contando com a referente a operações e a referente a selecção
						  $qt_colunas=7;
						  $contagem_colunas=0;
						  $header_col[1]="Data.";
						  $header_col[2]="Nome";
						  $header_col[3]="Valor Bruto";
						  $header_col[4]="Valor líquido";
						  $header_col[5]="Crédito";
						  $header_col[6]="Id CB";
						  $header_col[7]="Telemóvel";
						  $header_col[8]="GGG";
						  $header_col[9]="HHH";
						  $header_col[10]="III";
						  $header_col[11]="Selecção";
						  $header_col[12]="Acção";
						  //campos a mostrar nas colunas
						  $campo[1]="data";						  
						  $campo[2]="nome";						  
						  $campo[3]="v_bruto";						  
						  $campo[4]="v_liquido";						  
						  $campo[5]="v_credito";						  
						  $campo[6]="id";						  
						  $campo[7]="estado_pago";						  
						  $campo[8]="id";						  
						  $campo[9]="id";						  
						  $campo[10]="id";	
						  $alinhar_celula[1]="center";		
						  $alinhar_celula[2]="left";		
						  $alinhar_celula[3]="center";		
						  $alinhar_celula[4]="center";		
						  $alinhar_celula[5]="center";		
						  $alinhar_celula[6]="center";		
						  $alinhar_celula[7]="center";		
						  $alinhar_celula[8]="center";		
						  $alinhar_celula[9]="center";		
						  $alinhar_celula[10]="center";		
						  $alinhar_celula[11]="center";		
						  $alinhar_celula[12]="right";	
						  $width_celula[1]=100;							  						  						  						  						  						  						  						  						  						  						  						  					  
						  $width_celula[2]=360;							  						  						  						  						  						  						  						  						  						  						  						  					  
						  $width_celula[3]=100;							  						  						  						  						  						  						  						  						  						  						  						  					  
						  $width_celula[4]=100;							  						  						  						  						  						  						  						  						  						  						  						  					  
						  $width_celula[5]=100;							  						  						  						  						  						  						  						  						  						  						  						  					  
						  $width_celula[6]=70;							  						  						  						  						  						  						  						  						  						  						  						  					  
						  $width_celula[7]=70;							  						  						  						  						  						  						  						  						  						  						  						  					  
						  $width_celula[8]=100;							  						  						  						  						  						  						  						  						  						  						  						  					  
						  $width_celula[9]=100;							  						  						  						  						  						  						  						  						  						  						  						  					  
						  $width_celula[10]=100;							  						  						  						  						  						  						  						  						  						  						  						  					  
						  $width_celula[11]=90;							  						  						  						  						  						  						  						  						  						  						  						  					  
						  $width_celula[12]=90;							  						  						  						  						  						  						  						  						  						  						  						  					  
						  $campos_concatenados="";
						  $j=0;
						  $borda_tabela=0;
						  


?>
<!-- ---------------------------------------- CONTAINER HEADER HTML ----------------------------------------    --> 

<?php include('bo_1_init_html_header.php');?>

<!-- ---------------------------------------- INCLUDES INITS ----------------------------------------    -->
<?php
$d=dirname(__FILE__);
/*$path = '/i/_func';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);*/?>
<?php //include("bo_1_init_sys.php");?>
<?php //include("bo_1_init_sql.php");?>
<!-- ---------------------------------------- CONTAINER TABELA DE DADOS ----------------------------------------    -->   
<!--	H1	H1	H1	H1	H1	H1	H1	H1	-->
<h1><?php echo($designacao);?></h1>
<input readonly="readonly" style="visibility:visible; border-style:dashed" size="90" maxlength="250" type="text" id="contagem"></input>

<!-- ---------------------------------------- CONTAINER MENU MAIN ----------------------------------------    --> 
<?php 
include("bo_1_menu_003.php");
?>
<!-- ---------------------------------------- CONTAINER MENU MAIN ----------------------------------------    --> 

<form id="frm_<?php echo($ficheiro);?>" name="frm_<?php echo($ficheiro);?>" action="<?php echo($ficheiro_alvo_post_get);?>.php" method="get">
<table border="<?php echo($borda_tabela);?>" cellpadding="4" cellspacing="1" bgcolor="#ffffff" align="center" width="100%">

<tr><td colspan="12" align="right">
<?php
//check de filtro
if($ficheiro_filtro_activo==1){echo ("<a href=\"".($ficheiro_filtro).".php\" target=\"\">Filtro</a>");}else{};
?>
&nbsp;&nbsp;
</td>
</tr>

<tr height="25">
<?php
//header da tabela
/*
for($j=1;$j<=$qt_colunas;$j++){
echo("<td class=\"headerTabela\">&nbsp;".$header_col[$j]."</td>");
}
echo("<td class=\"headerTabela\">&nbsp;".$header_col[11]."</td>");
echo("<td class=\"headerTabela\">&nbsp;".$header_col[12]."</td>");
*/
?>
</tr>
<?php
$ij=1;
$ij_2=1;
$arr_cb=array();
$arr_cb_telem=array();
$arr_cb_email=array();
//para a concatenação de 1 array com ao fim de ser ordenado
$arr_cb_full_data=array();
include("bo_2_colab_vendas_1_720_dias_loop_cb.php");
echo "<br>Qt registos: ".$num_total_registos_201105180432." [ Array size: ".sizeof($arr_cb)."]";
for($ij=1;$ij<sizeof($arr_cb);$ij++){
					//echo "<br>".$ij."  -  ".$arr_cb[$ij];
					$diff=0;
					$diff_2=0;
					$id_alvo=$arr_cb[$ij];
					$id_alvo_telem=$arr_cb_telem[$ij];
					$estado_de_publicacao=""; -
					$id_programado=0;
					$id_artista=0;
					$intRowCount = 0;
					$classeCss="";
					$valorCelula="";
					$query="";
					$resultado="";
					$num_total_registos=0;
					$qt_registos_p_pagina=2000000000;
					$inicio_de_consideracao_do_rs=0;
					$total_paginas=0;
					$pagina_corrente=0;
					$pagina_inicial=0;
					$criterio="";
					$criterio_temp_da_page=sql_20110518_0327_default_sql($id_alvo);
					$criterio=$criterio_temp_da_page;	
					$num_total_registos=0;		
					$FLAG_RS_1_REG=0;						
					//debug
					if(isset($_SESSION['nivelDeAcesso']))
					{
					if ($_SESSION['nivelDeAcesso']<5)
					{
					echo "<font color=\"#ff0000\">";
					echo $criterio."<br>";
					echo "</font>";
					}else{}}
					// Erro
					$msg[0] = "Conexão off";
					$msg[1] = "Não foi possível selecionar a BD";
					// conexão
					$conexao = mysql_pconnect($mySqlHost,$mySqlUser,$mySqlPw) or die($msg[0]);
					mysql_select_db($mySqlDB,$conexao) or die($msg[1]);
					// consulta SQL
					$query = sql_20110518_0327($criterio,$inicio_de_consideracao_do_rs,$qt_registos_p_pagina);
					$resultado = mysql_query($query,$conexao);
					$num_total_registos=mysql_num_rows($resultado);
					//echo "<br>num_total_registos".$num_total_registos;
					if ($num_total_registos>0){
						$FLAG_RS_1_REG=0;

										while ($linha = mysql_fetch_array($resultado) AND $FLAG_RS_1_REG==0) {
									
															  echo "";
															  if ($intRowCount % 2 == 0) {
																$classeCss = "rowImpar";
															  }
															  else {
																   $classeCss = "rowPar";
															 }
																$classeCss2 = "TrAmarelo";
																$classeCss3 = "TrVermelho";
																$classeCss4 = "TrVerde";
																//%classeCss5 = "\"TrAzul\"";  
																//%classeCss6 = "\"TrCinza\"";
															 ?>
															<?php 
															$id_programado=$linha['id'];
															$contagem_colunas=0;
															
															$startDate = $linha['stamp'];
															$endDate = time();
															$diff=round((abs($endDate-$startDate)/60/60/24),0);
															//print("<br>Dias: ".$diff);	
															if($diff<720 AND $diff>1){
																				//print("<br><strong>Lembrete:</strong> ".$diff);	






																						  //echo $linha[$campo[1]]."";
																						  $startDate =$linha['stamp'];
																						  $endDate = time();
																						  $diff_2=round((abs($endDate-$startDate)/60/60/24),0);
																						  //print("<br><strong>Dias:</strong> ".$diff_2);
																						  //para a concatenação de 1 array com ao fim de ser ordenado
																						  if($diff_2>=720 OR $diff_2<=01){
																							  }else{
																								  //devido à ordenação de um array ser alfabética e não numérica
																								  if(strlen($diff_2."")<3){
																						  				$arr_cb_full_data[$ij]="DIAS: 0".$diff_2." |".$arr_cb_full_data[$ij];
																								  }else{
																									  	$arr_cb_full_data[$ij]="DIAS: ".$diff_2." |".$arr_cb_full_data[$ij];
																									  }
																						  }
																						  //print("<br><strong>Arr:</strong> ".$arr_cb_full_data[$ij]);		
																						  
																		  /*
																				?>
																				<tr height="30">
																				<?php //coluna	1
																				if($contagem_colunas<=$qt_colunas-1){
																				?>
																						  <td width="<?php echo $width_celula[1]; ?>" class="<?php echo $classeCss; ?>" align="<?php echo $alinhar_celula[1];?>">
																						  <?php 
																						  echo $linha[$campo[1]]."";
																						  $startDate =$linha['stamp'];
																						  $endDate = time();
																						  $diff_2=round((abs($endDate-$startDate)/60/60/24),0);
																						  print("<br><strong>Dias:</strong> ".$diff_2);
																						  //para a concatenação de 1 array com ao fim de ser ordenado
																						  $arr_cb_full_data[$ij]="DIAS DESDE A VENDA: ".$diff_2."-".$arr_cb_full_data[$ij];	
																						  print("<br><strong>Arr:</strong> ".$arr_cb_full_data[$ij]);															    
																						  ?>
																						  
																						  </td>
																				<?php //fim de coluna
																				}else{};
																				$contagem_colunas=$contagem_colunas+1;
																				?>
																				
																				
																				
																				<?php //coluna	2
																				if($contagem_colunas<=$qt_colunas-1){
																				?>
																						  <td width="<?php echo $width_celula[2]; ?>" class="<?php echo $classeCss; ?>" align="<?php echo $alinhar_celula[2];?>">
																						  <?php echo $linha[$campo[2]]."";?>
																						  &nbsp;&nbsp;
																						  <a href="bo_1_colab_ficha_grid.php?id_programado=<?php echo trim($linha['id_cb']);?>" target="_self">
																						  <?php echo "+";?>
																						  </a>				
																						  </td>    
																				<?php //fim de coluna
																				}else{};
																				$contagem_colunas=$contagem_colunas+1;
																				?>
																				
																				
																				
																				<?php //coluna	3
																				if($contagem_colunas<=$qt_colunas-1){
																				?>
																						  <td width="<?php echo $width_celula[3]; ?>" class="<?php echo $classeCss; ?>" align="<?php echo $alinhar_celula[3];?>"><?php echo $linha[$campo[3]]."";?></td>
																				<?php //fim de coluna
																				}else{};
																				$contagem_colunas=$contagem_colunas+1;
																				?>
																				
																				
																				
																				<?php //coluna	4
																				if($contagem_colunas<=$qt_colunas-1){
																				?>
																						  <td width="<?php echo $width_celula[4]; ?>" class="<?php echo $classeCss; ?>" align="<?php echo $alinhar_celula[4];?>"><?php echo $linha[$campo[4]]."";?></td>    
																				<?php //fim de coluna
																				}else{};
																				$contagem_colunas=$contagem_colunas+1;
																				?>
																				 
																				 
																						  
																				<?php //coluna	5
																				if($contagem_colunas<=$qt_colunas-1){
																				?>
																						  <td width="<?php echo $width_celula[5]; ?>" class="<?php echo $classeCss; ?>" align="<?php echo $alinhar_celula[5];?>">
																						  <?php 
																						  echo $linha[$campo[5]]."";
																						  ?>
                                                                                          </td>    
																				<?php //fim de coluna
																				}else{};
																				$contagem_colunas=$contagem_colunas+1;
																				?>
																						  
																						  
																				
																				<?php //coluna	6
																				if($contagem_colunas<=$qt_colunas-1-1){
																				?>
																				<td width="<?php echo $width_celula[6]; ?>" class="<?php echo $classeCss; ?>" align="<?php echo $alinhar_celula[6];?>">
																				<?php 
																				echo $id_alvo."";
																				?>
																				</a>		                                                                      
																				</td>    
																				<?php //fim de coluna
																				}else{};
																				$contagem_colunas=$contagem_colunas+1;
																				?>
																				
																				
																						  
																				<?php //coluna	7
																				if($contagem_colunas<=$qt_colunas-1){
																				?>
																						  <td width="<?php echo $width_celula[7]; ?>" class="<?php echo $classeCss; ?>" align="<?php echo $alinhar_celula[7];?>">
																						  <?php 
																						  echo $id_alvo_telem;
																						  ?>
                                                                                          </td>    
																				<?php //fim de coluna
																				}else{};
																				$contagem_colunas=$contagem_colunas+1;
																				?>
																						  
																						  
																						  
																				<?php //coluna	8
																				if($contagem_colunas<=$qt_colunas-1){
																				?>
																						  <td width="<?php echo $width_celula[8]; ?>" class="<?php echo $classeCss; ?>" align="<?php echo $alinhar_celula[8];?>"><?php echo $linha[$campo[8]]."";?></td>    
																				<?php //fim de coluna
																				}else{};
																				$contagem_colunas=$contagem_colunas+1;
																				?>
																				
																				
																				
																				<?php //coluna	9
																				if($contagem_colunas<=$qt_colunas-1){
																				?>
																						  <td width="<?php echo $width_celula[9]; ?>" class="<?php echo $classeCss; ?>" align="<?php echo $alinhar_celula[9];?>"><?php echo $linha[$campo[9]]."";?></td>    
																				<?php //fim de coluna
																				}else{};
																				$contagem_colunas=$contagem_colunas+1;
																				?>
																				
																				
																				
																				<?php //coluna	10
																				if($contagem_colunas<=$qt_colunas-1){
																				?>
																						  <td width="<?php echo $width_celula[10]; ?>" class="<?php echo $classeCss; ?>" align="<?php echo $alinhar_celula[10];?>"><?php echo $linha[$campo[10]]."";?></td>    
																				<?php //fim de coluna
																				}else{};
																				$contagem_colunas=$contagem_colunas+1;
																				?>
																				
																				
																				
																				<?php //coluna	11
																				if(1==1){
																				?>
																						  <td width="<?php echo $width_celula[11]; ?>" class="<?php echo $classeCss; ?>" align="<?php echo $alinhar_celula[11];?>">
																						  <div align="center"><font size="1"><b><font face="Verdana, Arial, Helvetica, sans-serif" color="#000000"> 
																						  <input type="radio" name="id_programado" value="<?php echo $id_programado;?>"></input>
																						  </font> </b></font></div>
																						  </td>
																				<?php //fim de coluna
																				}else{};
																				?>
																				
																				
																				<?php //coluna	12
																				if(1==1){
																				?>
																						  <td width="<?php echo $width_celula[12]; ?>" class="<?php echo $classeCss; ?>" align="<?php echo $alinhar_celula[12];?>"">
																						  <?php
																						  echo "<a href=\"".$ficheiro_area_privada.".php?id_programado=$id_programado\" title=\"".$tooltip_legenda_area_privada."\">".$texto_legenda_area_privada."</a>";
																						  ?>
																						  </td>
																				<?php //fim de coluna
																				}else{};
																				?>
																				 </tr>
                                                                                 
                                                                                 
                                                                                 
   															<?php                                                                              
                                                                                 
                                                                                 */
															?>																				 
															<?php
															//
															}
															$intRowCount++;
										//	fim do loop (sair já no 1ª registo)
										break;
										 };
										 //fim da condição de RS>0 ou não
					}else{}
 //fim de listagem condicional
};

//	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	
//array para export 
$export_step_20110823_0008=1;
include("bo_2_colab_vendas_1_720_dias_export_mail.php");
//	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	
//	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	
//array para export
$export_step_20110823_0009=1;
include("bo_2_colab_vendas_1_720_dias_export_tel.php");
//	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	

	print "<br>";
	//desordenado
	for($ij_2=1;$ij_2<sizeof($arr_cb_full_data);$ij_2++){
	//print "<br>".$arr_cb_full_data[$ij_2];
	}
	sort($arr_cb_full_data);
	//print_r($arr_cb_full_data);
	print "<br>";
	//ordenado
	for($ij_2=1;$ij_2<sizeof($arr_cb_full_data);$ij_2++){
		if(left($arr_cb_full_data[$ij_2],4)=="DIAS"){
			print "<br><br><span style=\"font-size:14px\">-> ".$arr_cb_full_data[$ij_2]."</span>";
			//	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	
			//array para export
			$export_step_20110823_0008=2;
			include("bo_2_colab_vendas_1_720_dias_export_mail.php");
			//	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	
			//	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	
			//array para export
			$export_step_20110823_0009=2;
			include("bo_2_colab_vendas_1_720_dias_export_tel.php");
			//	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	
		}else{};
	}
?>

</table>

<P><br>

<?php
echo "<br><br>";
// libertar recursos
mysql_free_result($resultado);
mysql_close($conexao); 

?>

</form>

<!-- ---------------------------------------- CONTAINER TABELA DE DADOS ----------------------------------------    --> 

<!-- ---------------------------------------- CONTAINER FOOTER HTML ----------------------------------------    --> 
<?php
//	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	
//array para export
$export_step_20110823_0008=3;
include("bo_2_colab_vendas_1_720_dias_export_mail.php");
//	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	
//	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	
//array para export
$export_step_20110823_0009=3;
include("bo_2_colab_vendas_1_720_dias_export_tel.php");
//	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	
?>
<?php include('bo_1_init_html_footer.php');?>

 

Edited by 1Alex2
13 hours ago, Barand said:

A lot of code there for what is basically just


SELECT order_id
     , order_date
     , datediff(curdate(), order_date) as age
FROM order
WHERE datediff(curdate(), order_date) <= 720
ORDER BY age

Can you tel me how to do that? I'm not as expert. I didn't create this. The guy who did it vanish from earth and now I've this problem to solve.

Can you insert that solution in my code and paste it here? Please 

Thanks 

 

On 2/27/2019 at 5:22 PM, 1Alex2 said:

the order ... 010 .... 02, 021,022 .... 03, 031, 034
I looked and looked and I can not figure out where the problem is.

This is a remarkably common problem and it's all to do with Data Types

These are Character data and so are sorted alphabetically.

"010" < "02" < "021" < "022" < "03" < "031" < "034" 

What you want is more like these, which are numeric and so are sorted numerically. 

2 < 3 < 10 < 21 < 22 < 31 < 34

 

Somewhere along the line, your predecessor managed to get this value into a Character-based form instead of a numeric one, hence your current difficulties.  

Regards, 
   Phill  W.

On 2/28/2019 at 11:29 AM, 1Alex2 said:

Can you insert that solution in my code and paste it here? Please 

Unfortunately I do not know your database or table structures and I cannot see the query you are running because it is obscured behind a function. So all I can do (as I did) is provide an example and leave the rest to you.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.