This is probably so newbie. Basically im l
$count_versions = array();
while ($row = mysql_fetch_array($result))
{
$email_build = $row1['build_info'];
$count_versions[] = $email_build;
}
Now when I use print_r I get this
Array ( [2660] => 8 [2662] => 6 [2655] => 6 [2666] => 1 )
Which is perfect, now all I want to do is to output those values like
2660 - 8 votes
2662 - 6 votes
2655 - 6 votes
2666 - 1 votes
When I try this it seems to break up the values back into a full array which undoes my array_count_values but I am stumped
I realize this foreach loop makes no sense but its as close as I can get, any ideas how I can basically print it out like print_r does it so i can put it in a table later
$i=0;
foreach ($count_versions as $version => $nums)
{
$i++;
echo "Version: " . $nums . " - " . $count_versions . "<br />";
}
It looks so easy to do it with a foreach:
$count_versions = array ( "2660" => 8, "2662" => 6, "2655" => 6, "2666" => 1 );
foreach ($count_versions as $key => $value)
echo $key.' - '.$value.' votes<br>';
echo "Version: " . $version . " - " . $nums . " votes<br />";
Related
I have a card deck array:
$cards = array("2_of hearts" => 2, "3_of_hearts" => 3, "king_of_hearts" => 10);
And I want to echo the name of the card somewhere (example: 2_of_hearts) but also calculate something with the number attached to it, but I really can't seem to make it work. Also, I was unable to find a working answer for me.
Does anyone know how to do this?
If you use a foreach loop that provides you with the key and the value like this, you get both the 2_of hearts and the 2 as variables.
$cards = array("2_of hearts" => 2, "3_of_hearts" => 3, "king_of_hearts" => 10);
foreach ( $cards as $name => $value) {
echo $name . ' has a value of ' . $value.PHP_EOL;
$calc = $value + 5;
echo 'ADDED 5 - gives ' . $calc . PHP_EOL;
}
Result
2_of hearts has a value of 2
ADDED 5 - gives 7
3_of_hearts has a value of 3
ADDED 5 - gives 8
king_of_hearts has a value of 10
ADDED 5 - gives 15
Then you just do your calculation with the $value variable
using this syntax of foreach lets you get access to the keys of the array
$cards = array("2_of hearts" => 2, "3_of_hearts" => 3, "king_of_hearts" => 10);
foreach($cards as $key=> $value){
$key = explode("_",$key);
$cardname = $key[count($key)-1];
//echo $key[0] . " of " $cardname . "<br>";
echo $value . " of " $cardname . "<br>"; // edit after comments
}
NOTE: this is off-course if you sure your keys are always has the pattern #_of_cardname
I have two arrays that will always have the same length:
$unit = array('a','b','c','d','a','b','d');
$type = array('x','y','z','x','y','z','x');
Also there might be more type of units or types. Ex: Units might me 5 instead of 4(a,b,c,d) or type might be 5 instead of 3(x,y,z). But the length of two array are same.
Now with this data i want to create a table like this:
x y z
a 1 1
b 1 1
c 1
d 2
What I have done so far:
$TYPE = array_values(array_unique($type));
$UNIT = array_values(array_unique($unit));
These two will provide the first column and the top row.
echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse:collapse;'>";
echo "<tr><td>" . ' ' . "</td>";
$lengthtype = count($TYPE);
for($i=0; $i<$lengthtype; $i++)
{
echo "<td>" . $TYPE[$i] . "</td>";
}
echo "</tr>";
$unitlength = count($UNIT);
for($i=0; $i<$unitlength;$i++)
{
echo "<tr>";
echo "<td>" . $UNIT[$i] . "</td>";
echo "</tr>";
}
echo "</table>";
I figured I need to combine the two to create 2D array so I did this:
$newarray = array();
foreach($type as $key=>$val)
{
$newarray[$key][]=$val;
$newarray[$key][]=$unit[$key];
}
Now I cannot determine what is the efficient way to proceed?
If you guys can provide any hint that will be great.
UPDATED:
My question is how to create a table in the second block. Just to be clear those 1 and 2 are numbers and empty spaces can be zero.
This should work for you:
At the start you can use a for loop to loop through both arrays at once and create an array, where each $TYPE is the key of the subArray and in each subArray you count which $UNIT occurs at the same position.
Means you create an array like this:
Array
(
//↓ Each '$TYPE'
[x] => Array
(
[a] => 1
[d] => 2
//↑ ↑ Amount
//Each '$UNIT', which occurs at the same position as this type
)
[y] => Array
(
[b] => 1
[a] => 1
)
[z] => Array
(
[c] => 1
[b] => 1
)
)
After this it's simply just printing the data as you want it. For this you loop through all unique $UNITS which you get with array_unique(). And for each unit you loop through all unique $TYPE. There you just have to check if you have an element in the array or not and print it.
Code:
<?php
$UNIT = array('a','b','c','d','a','b','d');
$TYPE = array('x','y','z','x','y','z','x');
$count = [];
for($i = 0, $length = count($UNIT); $i < $length; $i++) {
if(!isset($count[$TYPE[$i]][$UNIT[$i]]))
$count[$TYPE[$i]][$UNIT[$i]] = 0;
$count[$TYPE[$i]][$UNIT[$i]]++;
}
echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse:collapse;'>";
echo "<tr><td></td><td>" . implode("</td><td>", array_unique($TYPE)) . "</td></tr>";
foreach(array_unique($UNIT) as $key){
echo "<tr><td>$key</td>";
foreach(array_unique($TYPE) as $v)
echo "<td>" . (isset($count[$v][$key]) ? $count[$v][$key] : "") . "</td>";
echo "<tr>";
}
echo "</table>";
?>
output:
x y z
a 1 1
b 1 1
c 1
d 2
sorry for the title of my question i really don't know how to emphasize my problem but here is the sample array:
and here is my code
foreach ($_POST['noofguest'] as $keyg => $valueg) {
echo $valueg. "<br />";
}
foreach($_POST['room_no'] as $key => $value){
foreach($value as $key2 => $value2){
echo $value2 . " has " . $valueg . "<br />";
}
}
and the result of this is:
1
2
56 has 1
57 has 1
but this is not the result that i want, what i want is
1
2
56 has 1
57 has 2
is this possible??
For the second loop, you want to receive the same index of the room_no but only inside the noofguest array. So that's exactly what you should do:
foreach($_POST['room_no'] as $key => $value)
foreach($value as $key2 => $value2)
echo $value2 . " has " . $_POST['noofguest'][$key2] . "<br />";
This question already has answers here:
How can I use PDO to fetch a results array in PHP?
(2 answers)
Closed 2 years ago.
I'm generally confused about the proper way to get data from a query once you execute. My end goal is to be able to show a list of all the records, but I'm not sure how to do it. I've tried a couple of different things but none have given me quite what I'm looking for. For example, when I search for Batman (which should have a record for [Batman, 500, 1993], and a record for [Batman, 499, 1993]) here's what I've tried:
$search->execute();
$result = $search->fetch(PDO::FETCH_ASSOC);
print_r($result);
Which gives me: Array ( [comicTitle] => Batman [comicIssue] => 500 [releaseDate] => 1993 )
That's more or less the format I'm looking for except that it's only one record instead of all of them.
$search->execute();
$result = $search->fetchAll(PDO::FETCH_ASSOC);
print_r($result);
Which gives me: Array ( [0] => Array ( [comicTitle] => Batman [comicIssue] => 500 [releaseDate] => 1993 ) [1] => Array ( [comicTitle] => Batman [comicIssue] => 499 [releaseDate] => 1993 ) )
$search->execute();
$result = $search->fetch(PDO::FETCH_ASSOC);
foreach($result as $row) {
echo $row['comicTitle']."<BR>";
echo $row['comicIssue']."<BR>";
echo $row['releaseDate']."<BR>";
This echos 3 B's, 3 5's, and 3 1's with an illegal string offset warning between each.
So if I wanted to echo a line reading
"Title: " . $whatevertitle . " Issue: " . $whateverissue . " Release: " $whateverelease
for each record found, how do I go about actually getting those values from the returned array?
using ->fetch(PDO::FETCH_ASSOC)
$search->execute();
while($row = $search->fetch(PDO::FETCH_ASSOC)){
echo "Title: " . $row['comicTitle']."<BR>";
echo "Issue: " . $row['comicIssue']."<BR>";
echo "Release: " . $row['releaseDate']."<BR>";
}
using ->fetchAll(PDO::FETCH_ASSOC)
$search->execute();
$result = $search->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $row) {
echo "Title: " . $row['comicTitle']."<BR>";
echo "Issue: " . $row['comicIssue']."<BR>";
echo "Release: " . $row['releaseDate']."<BR>";
}
I am using a db query that takes in a state and city then spits out 10 fields. Currently I can only see those fields by using print_r. I tried a suggestion on the php manual site a for loop to print the fields however I can't get it to work properly. Here is the code:
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
print_r($row)."</p>";
$arrayLength = count($row);
for ($i = 0; $i < $arrayLength; $i++){
echo "arrayName at[" . $i . "] is: [" .$row[$i] . "]<br>\n";
}
}
}
And this is the result:
Array ( [id] => 1299 [zip_code] => 04011 [city] => Brunswick [county] => Cumberland [state_name] => Maine [state_prefix] => ME [area_code] => 207 [time_zone] => Eastern [lat] => 43.9056 [lon] => -69.9646 ) ....
arrayName at[0] is: []
arrayName at[1] is: []
arrayName at[2] is: []
arrayName at[3] is: []
arrayName at[4] is: []
arrayName at[5] is: []
arrayName at[6] is: []
arrayName at[7] is: []
arrayName at[8] is: []
arrayName at[9] is: []
Any ideas why I am not able to properly print the fields and their values? Also my code fails if the query returns more than one row as the current code doesn't really accommodate it.
I put $i in the body of the for loop to see if it was working properly. Ideally I would have the field name where $i is and the value to the right of it after the colon.
you are fetching with mysql_fetch_assoc so
change loop into
foreach($row as $key => $value){
echo "Array key : $key = $value <br/>";
}
Your array keys are 'id', 'zip_code' etc. There's nothing in the 0, 1 etc. indexes of the array.
foreach ($row as $key => $value) {
echo "arrayName at[" . $key . "] is: [" . $value . "]<br>\n";
// which is the same as:
echo "arrayName at[" . $key . "] is: [" . $row[$key] . "]<br>\n";
}
Yes, because that returns an associative array
That means that you have to access the elements like so:
$row["id"] for instance
What you want is this
foreach($row as $key => $value)
echo "arrayName at[" . $key . "] is: [" .$value . "]<br>\n";
use mysql_fetch_array() instead of mysql_fetch_assoc()
the mysql_fetch_assoc() will return an associative array and only accessable via $row['name'].
With mysql_fetch_array() you can fetch an associative array, a numeric array, or both.
take a look here: http://www.php.net/manual/en/function.mysql-fetch-array.php