Séparer une variable en 2 avec limite

WRInaute occasionnel
Salut à tous,

J'ai une fonction php que je souhaiterais afficher sur 2 lignes en mettant les 10 premiers sur la ligne 1 et la suite sur la ligne 2.

Voici ma ligne de code:

Code:
function get_cats() {
		global $settings, $prefix, $root_path;
				
		$cat_data = "";
		$res = mysql_query("select * from {$prefix}category WHERE cat_parent_id=0 ORDER BY cat_name") or report();
		if(mysql_num_rows($res)==0) echo "no data";
	
		else {

			$tot = mysql_num_rows($res);
			$spannum = $tot+2;

			for($i=0;$i<$tot;$i++) {
				$row = mysql_fetch_assoc($res);

				$cat_url = $settings['set_seo_onoff'] =='off' 
						? "$root_path/".plaintext($row['cat_name'])."-$row[cat_id].html"
						: "$root_path/category.php?id=$row[cat_id]";

				$cat_data .= "<a href=$cat_url>$row[cat_name]</a>";		
				
			}
		}

On comprend qu'il s'agit de cette variable que je veux découper en 2 avec un limite (10) pour la ligne 1.

Code:
$cat_data .= "<a href=$cat_url>$row[cat_name]</a>";

C'est possible de m'aider à intégrer cela ? merci :D
 
WRInaute occasionnel
Salut,

Regarde peut être tu coté de la fonction php array_chunk(), ça n'irai pas pour ce que tu souhaite faire ?
 
WRInaute occasionnel
Merci, mais je pense qu'il est plus simple de réaliser quelque chose dans le style:

En faite je duplique ma fonction et j'obtiens donc 2 fonctions que je peux mettre dans 2 lignes différentes, et en jouant sur : for($i=0;$i<7;$i++), je sépare les données le problème est ici que je me retrouve avec les mêmes données.

Bon c'est pas super clair, voici le code:

Fonction 1:

Code:
function get_cats() {
		global $settings, $prefix, $root_path;
				
		$cat_data = "";
		$res = mysql_query("select * from {$prefix}category WHERE cat_parent_id=0 ORDER BY cat_name") or report();
		if(mysql_num_rows($res)==0) echo "Aucune donnée";
	
		else {

			$tot = mysql_num_rows($res);
			$spannum = $tot+2;

			for($i=0;$i<'4;$i++) {
				$row = mysql_fetch_assoc($res);

				$cat_url = $settings['set_seo_onoff'] =='off' 
						? "$root_path/".plaintext($row['cat_name'])."-$row[cat_id].html"
						: "$root_path/category.php?id=$row[cat_id]";

				$cat_data .= "<a href=$cat_url>$row[cat_name]</a>";		
				
			}
		}
		
		return $cat_data;			
    }

J'obtiens: catégorie 1 - catégorie 2 - catégorie 3 - catégorie 4

Fonction 2:

Code:
function get_cats2() {
		global $settings, $prefix, $root_path;
				
		$cat_data = "";
		$res = mysql_query("select * from {$prefix}category WHERE cat_parent_id=0 ORDER BY cat_name") or report();
		if(mysql_num_rows($res)==0) echo "Aucune donnée";
	
		else {

			$tot = mysql_num_rows($res);
			$spannum = $tot+2;

			for($i=5;$i<$tot;$i++) {
				$row = mysql_fetch_assoc($res);

				$cat_url = $settings['set_seo_onoff'] =='off' 
						? "$root_path/".plaintext($row['cat_name'])."-$row[cat_id].html"
						: "$root_path/category.php?id=$row[cat_id]";

				$cat_data .= "<a href=$cat_url>$row[cat_name]</a>";		
				
			}
		}
		
		return $cat_data;			
    }

J'obtiens: catégorie 1 - catégorie 2 - catégorie 3 - catégorie 4
au lieu de

catégorie 5 - catégorie 6 - catégorie 7 - catégorie 8

Comment est ce que je dois m'y prendre?

Merci,
 
WRInaute occasionnel
Je comprend pas pourquoi tu t'obstine à vouloir passer par des boucles alors qu'il y a des fonctions faites pour ça.

Code:
function get_cats() {
	global $settings, $prefix, $root_path;
      
	$res = mysql_query("select * from {$prefix}category WHERE cat_parent_id=0 ORDER BY cat_name") or report();
	if(mysql_num_rows($res)==0) {
		echo "Aucune donnée";
	else {
		$cats = mysql_fetch_array($res);
		$cats = array_chunk($cats, 4, true);          
      print_r($cats);
      }
}

Normalement ça te retourne un tableau avec en $cats[0] => catégories 1 à 4 et en $cats[1] => catégories 5 à 8
Tableau que tu peux exploiter facilement après.
C'est pas bon ?
 
WRInaute occasionnel
Comme tu l'aura facilement compris, je suis néophyte :D
A la base référenceur je me lance dans la technique avec beaucoup de mal.

Et là je ne vois pas comment intégrer la variable array_chunk() comme il le faut.

A voir ce que tu m'a fait, il manque l'essentiel à savoir:

Code:
$cat_url = $settings['set_seo_onoff'] =='off'
                  ? "$root_path/".plaintext($row['cat_name'])."-$row[cat_id].html"
                  : "$root_path/category.php?id=$row[cat_id]";

            $cat_data .= "<a href=$cat_url>$row[cat_name]</a>";
 
WRInaute occasionnel
Quelques chose comme ceci fonctionne,

Code:
function get_cats() {
		global $settings, $prefix, $root_path;
				
		$cat_data = array();
		$res = mysql_query("select * from {$prefix}category WHERE cat_parent_id=0 ORDER BY cat_name") or report();
		while($row = mysql_fetch_assoc($res)) {
                $cat_url = $settings['set_seo_onoff'] =='off' 
						? "$root_path/".plaintext($row['cat_name'])."-" . $row['cat_id'] . ".html"
						: "$root_path/category.php?id=" . $row['cat_id'];
 
				$cat_data[] = "<a href=$cat_url>" . $row['cat_name'] ."</a>";		
				
			}
	
		return $cat_data;			
    }

Que j'intègre avec le code suivant:

Code:
$chunk_data = array_chunk(get_cats(), 5);
 
foreach ($chunk_data as $chunk) {
   echo '<div>' . implode('', $chunk) . '</div>';
}

Mais là je bug pour l'intégrer dans mon fichier .tpl vu que c'est du php :D

Avant je faisait dans mon index.php

Code:
$cats = get_cats();

Et dans mon index.tpl j'appelais la fonction comme ceci

Code:
$cats


Allez un dernier effort !!
Merci pour la fin
 
Discussions similaires
Haut