I'm trying to output the result of an array I fill from an SQL database.
<?php
if(mysqli_num_rows($Difference))
{
while($row = mysqli_fetch_assoc($Difference))
{
$block[] = $row["time"]." | ".$row["d_bl"];
}
}?>
When I "vardump" the array, it contains all the proper values I expect.
I want to output this array now in a simple list with breaks.
When I use "return" with the array, I only get one result due the automatic escape. When I print the array, I get 150 times the array instead of only the 150 once, due the loop.
Here is the solution from #Nigel Ren
After your while loop has finished -
echo implode("<br />", $block);
(Assuming web page output - replace "<br />" with PHP_EOL if not)
Related
In my Php code I have an array object $myArrayIbject that I want to get its values. I know it is not a one dimensional array. That's what I know for sure.
When I run
echo gettype($myArrayIbject);
It returns Object.
When I run
echo count($myArrayIbject);
It returns 1632.
When I run
var_dump( $myArrayIbject);
It returns
object(variant)#3(0){ }
When I run
variant_get_type($myArrayIbject)
It returns 8209.
The other thing I have observed is that from $myArrayIbject[0] all the way to $myArrayIbject[1631] it returns integer values when I run the below code
for ($i=0; $i< count($myArrayIbject); $i++) {
echo "Value at ".$i." is ". $myArrayIbject[$i]."<br/>";
}
I know this is not the way to access all its values. I am looking for a way to extract and access all its values.
You can use this
foreach($myArrayIbject as $index=>$value)
echo "Value at ".$index." is ". $value."<br/>";
}
how to get data returned by mysql_fetch_object() in a variable. i am using below mentioned code....
i am trying to fetch dates from DB based on condition and also want that the value to be stored in variable one by one . But when this code executes it print all the values returned by stdobj.
<?php
$connect=mysql_connect('host','user','xyz') ;
echo mysql_error();
$db=mysql_select_db("file_date",$connect);
echo mysql_error();
$append_d="00:00:00";
$date_test=date('Y-m-d', strtotime("-20 days"));
$full_d="$date_test "."$append_d";
$query_d=mysql_query("SELECT time as time_f FROM table WHERE date>='$full_d'");
$data_s = "";
while ($row_s = mysql_fetch_object($query_d)) {
$x_gmail=$row_s->time_f;
echo $x_gmail;
}
?>
below is the sample output
-:2013-09-08 13:45:292013-09-09 14:19:192013-09-10 17:30:072013-09-12 11:39:492013-09-12 12:09:492013-09-12 14:09:482013-09-12 16:09:472013-09-12 18:04:472013-09-12 19:09:462013-09-13 00:05:442013-09-13 17:09:372013-09-14 00:04:342013-09-15 22:59:542013-09-16 12:05:492013-09-16 13:10:482013-09-16 15:35:472013-09-16 19:05:462013-09-17 13:44:382013-09-18 00:05:342013-09-18 12:05:292013-09-18 14:34:272013-09-18 16:45:272013-09-18 17:35:272013-09-19 13:45:182013-09-19 21:05:152013-09-20 13:46:082013-09-23 12:59:392013-09-23 13:45:372013-09-24 13:45:272013-09-25 00:10:23
i want that for 1st value 2013-09-08 13:45:29 can be assigned to any variable so when i echo variable x it will print 2013-09-08 13:45:29 not the whole object string.
You're now printing each date string inside the while loop. That's not the whole object string -- they're individual strings, but it just appears as a big chunk since you're not using linebreaks to separate them.
If you want only one date string, then you can use LIMIT clause in your SQL query:
SELECT time as time_f FROM table WHERE date>='$full_d' LIMIT 1;
But however, if you want to have all the date string, but want to echo only the first one, then you could store them in an array and re-use it later:
while ($row_s = mysql_fetch_object($query_d)) {
$x_gmail[] = $row_s->time_f;
}
And you can use it like so:
$x = $x_gmail[0];
echo $x;
That'll just print the first item.
I have two arrays built from an XML response to an api. One array gives me the product info form one store and the other array gives me just the stock level in another. It looks like this:
foreach($filteredStock as $t=>$k){
$codeFirst[] = $k['code'];
echo '<tr><td>'.$t;
echo '</td><td>'.$k['desc'];
echo '</td><td>'.$k['family'];
echo '</td><td>'.$_POST['filterSelect'];
echo '</td><td>'.$k['onOrder'];
echo '</td><td>'.$k['cost'];
echo '</td><td>'.$k['sell'];
echo '</td><td>'.$k['invStore'];
echo '</td>';
}
$output = array();
$result = array_intersect_key($mainArray, array_flip($codeFirst));
foreach($result as $results=>$rValues){
echo '<td>'.$rValues['inv'];
echo '</td>';
echo '</tr>';
}
I want to display it so that it looks like this in the table:
code / desc / family / filteredClass / onOrder / cost / sell / invStore / invwarehouse
Because I need the second foreach loop to grab the values of the second array it causes a problem trying to get it to repeat on each line with the other array. Any suggestions?
Rather than echoing output each time the loop iterates, why don't you build up the table how you want it and then output it when you have it arranged properly?
It's not clear to me from the code exactly what you are trying to do, but if it's an organisation problem then why don't you store the values in seperate arrays and then combine then and arrange them appropriately before outputting them?
Using echo immediately for each loop iteration limits your options somewhat.
When sending data from a form to a second page, the value of the session is always with the name "Array" insteed of the expected number.
The data should get displayed in a table, but insteed of example 1, 2, 3 , 4 i get : Array, Array, Array.
(A 2-Dimensional Table is used)
Is the following code below a proper way to "call" upon the stored values on the 2nd page from the array ?
$test1 = $_SESSION["table"][0];
$test2 = $_SESSION["table"][1];
$test3 = $_SESSION["table"][2];
$test4 = $_SESSION["table"][3];
$test5 = $_SESSION["table"][4];
What exactly is this, and how can i fix this?
Is it some sort of override that needs to happen?
Best Regards.
You don't need any sort of override. The script is printing "Array" rather than a value, because you're trying to print to the screen a whole array, rather than a value within an array for example:
$some_array = array('0','1','2','3');
echo $some_array; //this will print out "Array"
echo $some_array[0]; //this will print "0"
print_r($some_array); //this will list all values within the array. Try it out!
print_r() is not useful for production code, because its ugly; however, for testing purposes it can keep you from pulling your hair out over nested arrays.
It's perfectly fine to access elements in your array by index: $some_array[2]
if you want it in a table you might do something like this:
<table>
<tr>
for($i = 0 ; $i < count($some_array) ; $i++) {
echo '<td>'.$some_array[$i].'</td>';
}
</tr>
</table>
As noted, try
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
That should show you what's in the session array.
A 2-dimensional table is just an array of arrays.
So, by pulling out $_SESSION["table"][0], you're pulling out an array that represents the first row of the table.
If you want a specific value from that table, you need to pass the second index, too. i.e. $_SESSION["table"][0][0]
Or you could just be lazy and do $table = $_SESSION["table"]; at which point $table would be your normal table again.
A nice way ...
<?php
foreach ($_SESSION as $key => $value) {
echo $key . " => " . $value . "<br>";
}
?>
Is it possible to echo or print the entire contents of an array without specifying which part of the array?
The scenario: I am trying to echo everything from:
while($row = mysql_fetch_array($result)){
echo $row['id'];
}
Without specifying "id" and instead outputting the complete contents of the array.
If you want to format the output on your own, simply add another loop (foreach) to iterate through the contents of the current row:
while ($row = mysql_fetch_array($result)) {
foreach ($row as $columnName => $columnData) {
echo 'Column name: ' . $columnName . ' Column data: ' . $columnData . '<br />';
}
}
Or if you don't care about the formatting, use the print_r function recommended in the previous answers.
while ($row = mysql_fetch_array($result)) {
echo '<pre>';
print_r ($row);
echo '</pre>';
}
print_r() prints only the keys and values of the array, opposed to var_dump() whichs also prints the types of the data in the array, i.e. String, int, double, and so on. If you do care about the data types - use var_dump() over print_r().
For nice & readable results, use this:
function printVar($var) {
echo '<pre>';
var_dump($var);
echo '</pre>';
}
The above function will preserve the original formatting, making it (more)readable in a web browser.
var_dump() can do this.
This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
http://php.net/manual/en/function.var-dump.php
I think you are looking for print_r which will print out the array as text. You can't control the formatting though, it's more for debugging. If you want cool formatting you'll need to do it manually.
This is a little function I use all the time its handy if you are debugging arrays. Its pretty much the same thing Darryl and Karim posted. I just added a parameter title so you have some debug info as what array you are printing. it also checks if you have supplied it with a valid array and lets you know if you didn't.
function print_array($title,$array){
if(is_array($array)){
echo $title."<br/>".
"||---------------------------------||<br/>".
"<pre>";
print_r($array);
echo "</pre>".
"END ".$title."<br/>".
"||---------------------------------||<br/>";
}else{
echo $title." is not an array.";
}
}
Basic usage:
//your array
$array = array('cat','dog','bird','mouse','fish','gerbil');
//usage
print_array("PETS", $array);
Results:
PETS
||---------------------------------||
Array
(
[0] => cat
[1] => dog
[2] => bird
[3] => mouse
[4] => fish
[5] => gerbil
)
END PETS
||---------------------------------||
You can use print_r to get human-readable output.
See http://www.php.net/print_r
Similar to karim's, but with print_r which has a much small output and I find is usually all you need:
function PrintR($var) {
echo '<pre>';
print_r($var);
echo '</pre>';
}
//#parram $data-array,$d-if true then die by default it is false
//#author Your name
function p($data,$d = false){
echo "<pre>";
print_r($data);
echo "</pre>";
if($d == TRUE){
die();
}
} // END OF FUNCTION
Use this function every time whenver you need to string or array it will wroks just GREAT.
There are 2 Patameters
1.$data - It can be Array or String
2.$d - By Default it is FALSE but if you set to true then it will execute die() function
In your case you can use in this way....
while($row = mysql_fetch_array($result)){
p($row); // Use this function if you use above function in your page.
}
You can use print_r to get human-readable output.
But to display it as text we add echo '<pre>';
echo '<pre>';
print_r($row);