1. 程式人生 > >php迴圈語句輸出

php迴圈語句輸出

1.使用$mysqli

<?php
	require '../data/conn.php';
	$sql = 'SELECT * FROM zy_category';
	$resulet = $mysqli->query($sql);
	
	$out = [];
	while (true) {
		$row = $resulet->fetch_assoc();
		if ($row == null) break;
		$out[] = $row;
	}



?>
//。。html
//輸出html 結構

		<ul class="checkbox-container" id="checkboxContainer">
			<li>
				<input id="a1" type="checkbox" name="chk_art" value="it1">
				<label for="a1">IT1</label>
			</li>
			<?php
				foreach ($out as $key => $value) {
					# code...
					echo "<li>";
					echo "<input id='a".$value['c_id']."' type='checkbox' name='chk_art' value='".$value['article_type']."'>";
					echo "<label for='a".$value['c_id']."'>".$value['article_type']."</label>";
					echo "</li>";
				}
				for ($i=0; $i < count($out); $i++) { 
					# code...
					// echo "for..".$out[$i]['article_type'];
				}
			?>
			
		</ul>