I am trying to create a PHP function that will echo info from a db. My col names look like banana[1], banana[2], apple[1], apple[2], apple[3], and so on.
The function will select the fruit
function fruits($fruit){
}
and then within that function I’ll loop through the fruits.
How do I echo these?
echo $fruit[$i];
obviously doesn’t work.
Pretty basic, but I can’t figure out. Concatenation kills me.
function ponctuation($section,$sect){
switch($section){
case 'apple':
$i=1;
break;
case 'banana':
$i=13;
break;
}
global $mysql_tablename;
global $FName;
global $Lname;
if(isset($mysql_tablename)){
$result = mysql_query("SELECT * FROM $mysql_tablename WHERE FName='$FName' AND Lname='$Lname'");
$row = mysql_fetch_array($result);
echo '<form method="post" action="ponctuation.php?'.${$section.$sect}.'_valider">';
echo '<ul>';
$query = mysql_query("SELECT * FROM `ponct_enonces` WHERE `section`='$section' AND `sect`='$sect'");
while($row_q = mysql_fetch_assoc($query)) {
if($row_q['enon']==0 && $row_q['senon']==0){
echo '<h2>'.$row_q['enonce'].'</h2>';
}
if($row_q['enon']==1){
echo '<h3>'.$row_q['enonce'].'</h3>';
echo '<textarea rows="3" cols="100" name="'.$i.'" wrap="physical">' . $row[$section[$i]] . '</textarea>';
}
if($row_q['senon']==1){
echo '<h4>'.$row_q['enonce'].'</h4>';
echo '<textarea rows="3" cols="100" name="'.$i.'" wrap="physical">' . $row[$section]. '</textarea>';
}
$i++;
}
echo '</ol>';
echo '<input type="submit" name="submit" value="Valider"/>';
echo '</form>';
}
}
The code is a little ugly, and some vars have French names, other have English names.
col names won’t be expanded into PHP array structures for you, e.g.
will give you the equivalent of
if you want to iterate those, you’ll have to get ugly:
which begs the question of… why? This is a horrible data structure to be using. Properly normalizing it into a ‘fruit attributes’ sub-table would save you this trouble, and also allow you to have
nattributes, rather than the fixed 1..100 range or whatever it is you have.