Jump to content
Larry Ullman's Book Forums

Ex1 - Html Content Multiple Cat


Recommended Posts

hello thanks for an answer.

 

Yes, it's only an application and the developement of "html content multiple cat" of ex 1.

 

 

For the query SELECT, I can not recover in the Form the registered tag of the bd . 
Only one appears and is always the last in the list. 
 
For select  the tag's field, I  use "GOUP_CONCAT (tag) and the result is a list of string of tags. 
ie : végétarien,classique
 
I transformed into array with the function preg_split with a comma: preg_split ("/ [\,] + /", $ tag); 
 
I created a foreach and I compare the name for the index recovered for :
 
la value : végétarien , tag_id = 1 ;  la value : classique , tag_id = 5
 
$tag("végétarien", 1);
tag $ ('classique', 5);
 
But no, 
only one result is cheked , the last one  :  tag $ ('classique', 5) 
 
I really do not understand where is the error; 
 
thank you for advice or a solution perhaps
 
best regards
<div class="checkbox form-control">
		<label for="tag" class="control-label">Tag</label>
		<?php 
		$qt="SELECT * FROM tag ORDER BY tag DESC";
		$r=mysqli_query($dbc,$qt);
			while ($rowt=mysqli_fetch_array($r, MYSQLI_ASSOC)) {?>
			<div class="checkbox <?php if(array_key_exists('tag', $update_page_errors)) echo ' has-error'; ?>">
					 	<label>
						 	<input type="checkbox" name="tag[]" id="<?php echo $rowt['tag'] ?>" value="<?php echo $rowt['tag_id'] ?>"
						 	<?php 
						 	if(isset ($tag) && ($tag_id== $rowt['tag_id']) ) echo !empty('checked="checked"') ? 'checked="checked"' : '';?>><?php echo $rowt['tag'] ?>
					 	</label>
				</div>
				
				
		    <?php if(array_key_exists('tag', $update_page_errors)) echo'<span class="help-bloc">'.$update_page_errors['tag'].'</span>'; ?>
		    
			<?php } ?>
	</div>	   

Link to comment
Share on other sites

Hello,

 

Book "Effortless E-Commerce with PHP and MySQL (2nd Edition)" -- Annexe -- html content multiple cat" of ex1- suite.

 

i find the soluce

is in the form checkbox with the in_array function

 

and transform the result of $tag

// php/sql query SELECT

GROUP_CONCAT(t.tag) AS tag

 

 

$tagNom = preg_split("/[\,]+/", $tag );
 
<div class="checkbox form-control">
		<label for="tag" class="control-label">Tag</label>
		<?php 
		$qt="SELECT tag_id, tag FROM tag ORDER BY tag DESC";
		$r=mysqli_query($dbc,$qt);
			while ($rowt=mysqli_fetch_array($r, MYSQLI_ASSOC)) {
			
			//print_r($rowt);
			
			$monTag_id = $rowt['tag_id'];
			$monTag = $rowt['tag'];
			
			?>
			<div class="checkbox <?php if(array_key_exists('tag', $update_page_errors)) echo ' has-error'; ?>">
					 	<label>
						 	<input type="checkbox" name="tag[]" id="<?php echo $monTag ?>" value="<?php echo $rowt['tag_id'] ?>"
						 	<?php 
						 	//print_r($tagNom);
						 	if( isset($tagNom) && in_array( $monTag , $tagNom ) ) echo !empty('checked="checked"') ? 'checked="checked"' : '';?>><?php echo $rowt['tag'] ?>
					 	</label>
				</div>
				
				
		    <?php if(array_key_exists('tag', $update_page_errors)) echo'<span class="help-bloc">'.$update_page_errors['tag'].'</span>'; ?>
		    
			<?php } ?>
	</div>	   

Cordialy

Link to comment
Share on other sites

Hello,

so now this part of reflexion and application is ok.

 

I find all code for the part of Extending the first site, 

 

"Placing HTML content in Multiple Category" p.406 .

 

I use Tag for Category.

 

Certainly, some ameliorations are possible, but it's match.

 

All suggestions are welcome.

 

//le code en extraits

 
 
if( isset($_GET['page_id']) && !$_POST ){
	//recuperer les infos
	$page_id = $_GET['page_id'];
			//SELECT	
			$q = "SELECT  les valeurs  FROM pages WHERE page_id=?";
			$stmt = mysqli_prepare($dbc, $q);
			//associe selon 
			mysqli_stmt_bind_param($stmt,'i', $page_id);
			mysqli_stmt_bind_result($stmt, $user_id, les valeurs, etc...);
			//
			$OK = mysqli_stmt_execute($stmt);
			mysqli_stmt_fetch($stmt);
			mysqli_stmt_free_result($stmt);
 
			//pour la table des tags
			$q = 'SELECT tag_id FROM pages_tag WHERE page_id=?';
			$stmt = mysqli_prepare($dbc, $q);
			//bind
			mysqli_stmt_bind_param($stmt,'i', $_GET['page_id']);
			mysqli_stmt_bind_result($stmt, $tag_id);
			$OK = mysqli_stmt_execute($stmt);
			//loop tag à récuperer (voir avec Formulaire)
			$selected_tags = array();
				while (mysqli_stmt_fetch($stmt) ) {
					$selected_tags[] = $tag_id;
				}
		}//end if GET
 
//et maintenant le formulaire à enregistrer
if(isset($_POST['update'])){
	$page_id = $_POST['page_id'];
 
	if(!empty($_POST['title']) ){
			$title = escape_data(strip_tags($_POST['title']), $dbc);
			}else{
			$update_page_errors['title'] = 'SVP, un titre';
			}
     //les autres valeurs à verifier
 
	if(empty($update_page_errors) ){
			//Update
			$q ="UPDATE pages SET VALEURS=? WHERE page_id=? ";
		         $stmt = mysqli_prepare($dbc, $q);
			mysqli_stmt_bind_param($stmt, 'iisssssssi', $VALEURS);
                  $title= strip_tags($_POST['title']);
	          $done = mysqli_stmt_execute($stmt);	
 
		//delete les valeurs existantes de tag
		$q = "DELETE FROM pages_tag WHERE page_id=?";
				$stmt = mysqli_prepare($dbc,$q);
				mysqli_stmt_bind_param($stmt, 'i', $_POST['page_id']);
				mysqli_stmt_execute($stmt);
 
		//inserer les nouvelles valeurs pour la table pages_tag
		if(isset($_POST['tag'])	&& is_numeric($_POST['page_id']) ){	
			$page_id = (int) $_POST['page_id'];
				foreach ($_POST['tag'] as $tag_id) {
				$values[]="($page_id, " . (int) $tag_id . ')';
				}
 if($values){		
	$qInsert ='INSERT INTO pages_tag (page_id, tag_id) VALUES '  . implode(',', $values);
			mysqli_query($dbc, $qInsert);
			}//fin values
		}//end if postTag et page_id
 
	}//END pas erreur $update
		else {
 echo 'Tous les champs doivent être remplis';
		}
 
} //ifPost UPDATE

best regards

cordialy

Link to comment
Share on other sites

 Share

×
×
  • Create New...