How would I make a table that takes a PHP array. The array is decided by a html input. The table has the actual values in the first column, length in the second column, and if it starts with 3 letters, 3 numbers or neither in the third column.
echo '<table>';
foreach ($array as $key => $value) {
echo '<tr><td>';
echo htmlspecialchars($array);
echo '</td><td>';
echo strlen($array);
echo '</td><td>';
//to work on
$trim = substr($array, 0, 3);
if(is_numeric($trim)){
echo 'numeric';
}elseif(is_string($trim)){
echo 'string';
}else{
echo 'else ?';
}
echo '</td></tr>';
}
echo '</table>';
PHP will interpret POST values with brackets in their name as arrays.
<form method="POST">
<input type="text" name="values[]" >
<input type="text" name="values[]" >
<input type="text" name="values[]" >
<button type="submit">Go</button>
</form>
Now... The solution presented here is only to get you started. The third column can be a bit tricky because a value of 'ab123c' will pass as string.
I will let you work the kinks the way you want them to be handled.
if(is_array($_POST['values'])){
echo '<table>';
foreach ($_POST['values'] as $key => $value) {
echo '<tr><td>';
echo htmlspecialchars($value);
echo '</td><td>';
echo strlen($value);
echo '</td><td>';
//to work on
$trim = substr($value, 0, 3);
if(is_numeric($trim)){
echo 'numeric';
}elseif(is_string($trim)){
echo 'string';
}else{
echo 'else ?';
}
echo '</td></tr>';
}
echo '</table>';
}
PS: Never trust user input ;)
I know the code is not squeaky clean, the goal was for it to be easy to understand.