Weedpacket - I replaced my print_r statement with the below for each loop
foreach ($types as list($a)) {
// Note that there is no $b here.
echo "$a\n";
}
but nothing writes to my page.
Weedpacket - I replaced my print_r statement with the below for each loop
foreach ($types as list($a)) {
// Note that there is no $b here.
echo "$a\n";
}
but nothing writes to my page.
Do any of the examples on the page that Weedpacket linked to use list() in the function call?
foreach() will explode the passed-in array into separate values (and keys if you ask it to) automatically.
foreach($array as $index => $value){
echo "<p>\$index: {$index}; \$value: {$value}</p>";
}
Also, I'm a bit confused about the note that there is no $b. Of course there's not, $b hasn't been defined.
maxxd - if I use your syntax it will print on screen the below
$index: 0; $value: Array
$index: 1; $value: Array
$index: 2; $value: Array
$index: 3; $value: Array
$index: 4; $value: Array
$index: 5; $value: Array
$index: 6; $value: Array
$index: 7; $value: Array
$index: 8; $value: Array
$index: 9; $value: Array
Ah - I see in your code that you're using mysqli_fetch_assoc(). I had only looked at the data you posted and skipped the actual code. You may have to change to this:
foreach($array as $index => $value){
echo "<p>\$index: {$index}; \$value: {$value[0]}</p>";
}
If that doesn't work, var_export() $value on each iteration to see what it actually contains.
maxxd --
If I try your foreach in the code above nothing is displayed on page and if I do
var_export($value);
in the foreach loop nothing is displayed on the page either.
Um... let's see your code. Your code would have worked if you hadn't stuck that spurious list
in there. @maxxd's would have worked as long as you remembered to replace $array
with $types
.
Weedpacket --
This is the code I currently have
<?php
$dblink = mysqli_connect("website", "user", "pass", "database");
if (mysqli_connect_errno()) {
echo "Could not connect to database: Error: ".mysqli_connect_error();
exit();
}
$sqlquery = " "SELECT employee FROM employees LIMIT 10";
if ($result = mysqli_query($dblink, $sqlquery)) {
while ($row = mysqli_fetch_assoc($result)) {
$types[] = $row['employee'];
}
mysqli_free_result($result);
}
mysqli_close($dblink);
foreach($types as $index => $value){
echo "<p>\$index: {$index}; \$value: {$value[0]}</p>";
}
//print_r(json_encode(array_values($types)));
?>
And this is what it outputs:
$index: 0; $value: A
$index: 1; $value: A
$index: 2; $value: A
$index: 3; $value: A
$index: 4; $value: B
$index: 5; $value: B
$index: 6; $value: B
$index: 7; $value: B
$index: 8; $value: B
$index: 9; $value: B
Okay: so why have you added [0]
to {$value[0]}
?
Weedpacket - i was trying the syntax from @maxxd
There's an extra quote in your query assignment. If that's the code you're actually running on the server you should be seeing errors, assuming you have error reporting turned on. Of course, if error reporting isn't turned on you shouldn't be seeing any output at all. So the question is - if my first code sample showed 'Array' as $value, but $value[0] shows only a single letter, then either the value of $value is an array of individual letters or something got changed somewhere along the way. Assuming the extra quotation mark is a copy/paste error, the code you've posted should work without the '[0]' after $value, as the first code I posted showed - but you've already said that didn't work...
No't a—good? Idea to-copy arbit-rary. Syntax; without, learning ... what (its) "for"!
bobgoblin If you read your own code, you'll see that you are collecting your database rows into a variable $types
. Try reading the docs on foreach like weedpacket suggested rather than just copying and pasting other people's code into your script.
In your case, $types
is an array and you can get at the contents of that array with a foreach loop like this:
foreach($types as $key => $one_element) {
echo "The $key th element of types is $one_element\n";
}