php - display array only if exists -
i made page users can see data entered them in database.
used:
$select = "select * texts user='".$user."' order date desc, id desc"; $result = mysql_query($select); $array = array(); while($show = mysql_fetch_assoc($result)) { $array[] = $show; } echo "<strong>".$array[0]['id']."</strong><br />"; echo "<strong>".$array[1]['id']."</strong><br />"; echo "<strong>".$array[2]['id']."</strong><br />"; echo "<strong>".$array[3]['id']."</strong><br />"; echo "<strong>".$array[4]['id']."</strong><br />";
code works, have less 10 values return, more.
if use , have 2 arrays return, get:
notice: undefined offset: 2 in ownposts.php on line 15 notice: undefined offset: 3 in ownposts.php on line 16 notice: undefined offset: 4 in ownposts.php on line 17
how it's possible echo $arrray[4]['id] if exist $array[4]?
i've tried with:
$zero = $array[0]; if(!empty($zero)) { echo "<strong>".$zero['id']."</strong><br />"; } $four = $array[4]; if(!empty($four)) { echo "<strong>".$five['id']."</strong><br />"; }
but doesn't work excepted , still return notice: undefined offsed: 4 in ownposts.php on line 17.
instead of do, this:
while($show = mysql_fetch_assoc($result)) { $array[] = $show; } echo "<strong>".$array[0]['id']."</strong><br />"; echo "<strong>".$array[1]['id']."</strong><br />"; echo "<strong>".$array[2]['id']."</strong><br />"; echo "<strong>".$array[3]['id']."</strong><br />"; echo "<strong>".$array[4]['id']."</strong><br />";
why not show whatever found mysql find it:
while($show = mysql_fetch_assoc($result)) { echo "<strong>".$show['id']."</strong><br />"; }
or if have other things array code showed us, use loop on newly created array, ie foreach.
Comments
Post a Comment