I am having trouble showing a row from a database which has a list of skills as an array which is delimited by a commas. Fully annotated code with explanation below is what i’ve done sor far. All help is much appreciated. Thanks in advance.
// DB Connection here
// Execute Query
$sql = "SELECT id, skills FROM p_info";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
// Delimit array by comma
$myArray = explode(',', $row['skills']);
// Store as array
$myArr = array($myArray);
/**
Assume the value of is skills row in the database is: type 1, type 2, type 3, type 4
i would like to show these values each on a seperate <li> element hence using a loop.
<li> type 1</li>
<li> type 2</li>
<li> type 3</li>
<li> type 4</li>
My guess would be to use foreach loop..but im a little confused how to go about this. Here whats i think logically:
**/
?>
<ul>
<?
foreach ( $myArr as $skills ) {
?>
<li><?=$myArr;?></li>
<?
} // End loop
?>
</ul>
<!-- Can someone tell me what im doing wrong? im fairly new to PHP but im a quick learner. :) -->
Remove
$myArr = array($myArray);and doforeach ( $myArray as $skills ) {instead.You are putting your array within a new array, so when you try to loop through it you end up with the original array.
Also change
<?=$myArr;?>to<?=$skills;?>, you are looping through$myArrayand storing every part in$skillstemporarily,$myArraydoesn’t change so it’s useless trying toechoit.