I used this but my array show blank.
foreach($page_data->result() as $text){ $i++;
echo $name="text".$i; //this line print ok.
$$name=array();
echo $$name['content']=$text->$content; //this line print ok.
print_r($text1);
} print_r($text1);
here i am tying to name the array dynamically as text1,text2,text3.......
but when i print $text1 it shows me a blank array.
can any one help me out with this.
It's simply a syntax ambiguity. $$name['content'] is understood as:
${$name['content']}
I.e. the name of the variable is supposed to be the value of $name['content'], which obviously doesn't exist, which actually leads to an error if you'd enable error reporting. You can solve this with:
${$name}['content'] = $text->$content;
However, you really should solve this by using an array instead of variable variables:
$texts[] = array('content' => $text->$content);
You're incrementing the index before you echo the contents of the variable, so if you only have 1 result, it will try and access an undefined index, you are also re initialising the name array every time you pass through the loop, you should not do this
Results
------------------
Num Content
0 I am a text post
If these were the results returned and you wanted to point to index i, then when your loop goes through this would happen:
i = 0;
i = 1;
assign values to name array
As there is only one result this would then happen
Results
------------------
Num Content
0 I am a text post
- - <----- i = 1 (null pointer exception?)
This would be why no values are showing in your array, try change your code to this:
// Declare i to point at the 0 index
$i = 0;
$name=array();
foreach($page_data->result() as $text){
$name[i]['content']=$text;
$i++;
}
Your name array will no add the text content to a new index with each pass of the loop, i.e. if you had 2 results
$name[0]['content'] = "This is a text post";
$name[1]['content'] = "Here is another post";
Hope this helps.
Related
I'm trying to echo a simple json_encoded file.
<?php
$allMessages = file_get_contents("chatmessages.txt");
$allMessagesArr = explode(PHP_EOL, $allMessages);
$newObj = [];
var_dump($allMessagesArr);
foreach ($allMessagesArr as $thisLine) {
// echo($thisLine . "\n");
if (empty($thisLine) ) {
} else {
$thisLineArr = explode("|", $thisLine);
$newObj[trim($thisLineArr[0])] = trim($thisLineArr[1]);
// echo("here comes another one ".$thisLineArr[0] . " : ". $thisLineArr[1]."\n");
}
}
$newObjForFront = json_encode($newObj);
echo($newObjForFront);
chatmessages.txt looks like this
bob|hello
jimmy|second try incoming again
sam|third try
bob|oh damn
I've echoed each individual line within the loop and the fourth element appears. However, when I echo $newObjForFront, it's missing the last element. Any ideas why?
When you create your final array $newObj in
$newObj[trim($thisLineArr[0])] = trim($thisLineArr[1]);
Your using the name as the index to the array. As array indexes have to be unique, this means in fact the last entry overwrites the first one, so your actual output is...
{"bob":"oh damn","jimmy":"second try incoming again","sam":"third try"}
So it is in fact the first message that is missing.
Edit:
If you wanted to just have all of the messages, then you could store them using
$newObj[] = [ "user"=> trim($thisLineArr[0]), "msg" =>trim($thisLineArr[1])];
Which would give you the output as...
[{"user":"bob","msg":"hello"},{"user":"jimmy","msg":"second try incoming again"},{"user":"sam","msg":"third try"},{"user":"bob","msg":"oh damn"}]
$newObj[trim($thisLineArr[0])] = trim($thisLineArr[1]); this line will replace value with the last message of any username. if any username have multiple messages then only last message will be stored in the array.
By creating multidimensional you can store multiple messages with the same username. Check below code that may help you
$newObj[][trim($thisLineArr[0])] = trim($thisLineArr[1]);
what I have to change to this part will work?
$array4Midfeld = array('$LM', '$DMEins', '$DMZwei', '$RM');
$array4Midfield[0] = "Test";
At the moment I get an error, regarding to the second line.
Error is... $LM not defined.
There is spelling mistake in array name on line number 2.
You have created array with $array4Midfeld name and accessing it with
$array4Midfield name on line number 2. That's why you are getting error.
try following code, It is working.
<?php
$array4Midfield = array($LM, $DMEins, $DMZwei, $RM);
$array4Midfield[0] = "Test";
print_r($array4Midfield);
?>
You have two problems with this:
Are you assigning variables as elements of the array? (ie $LM, $DMZwei, etc), or are they text? "$LM", "$DMZwei"
Your variables are not the same. Line 1 and Line 2 are two different vars: array4Midfeld and array4Midfield, are they supposed to be the same?
So are you testing that
`$array4Midfield[0] = "Test"
or
`$array4Midfield[0] = $LM
or
$LM = "Test"
Please assign Values to an array in this form. More over the array name too, am confuse whether they are the same or not. $array4Mittelfeld or $array4Midfield
$array4Mittelfield[0] = $LM;
$array4Mittelfield[1] = $DMEins;
$array4Mittelfield[2] = $DMZwei;
$array4Mittelfield[3] = $RM;
echo $array4Mittelfield[0];
or you can use loop to print all the values
Hope it helps
I have this array in php code. I want to have that whenever page is being called it should print value of first array index and when next time second value of array index and so on... what modification I could do? for now it´s printing everything when being called single time.
<html>
<?php
$addresses = array('ifcbxespra', 'ifcheqjbmea', 'ifcqiknsa', 'ifcqirtjla', 'ifcwqsrlmn', 'ifclmkmzhz','ifcwdujhgc','ifcihddngh','icffhzudcd','ifchnsqzgs','ifcgssqrhg');
foreach ($addresses as &$value) {
echo $value ;
}
?>
</html>
I'm not sure if I understood what you want. But if you want to print the first array's value when the page loads one time, the second array's value when the page loads another time and so on, you can do this:
<?php
if(!isset($addresses) || empty($addresses)){ //checks if the array is not initialized or if it's empty
$addresses = array('ifcbxespra', 'ifcheqjbmea', 'ifcqiknsa', 'ifcqirtjla', 'ifcwqsrlmn', 'ifclmkmzhz','ifcwdujhgc','ifcihddngh','icffhzudcd','ifchnsqzgs','ifcgssqrhg');
echo $addresses[0]; //print the first value
array_splice($addresses, 0, 1); //removes the first element of the array and reindexes it
}else{
echo $addresses[0]; //print the first value
array_splice($addresses, 0, 1); //removes the first element of the array and reindexes it
}
The logic behinds it is: if the array already exists and is not empty (it has values), print the first value and then remove it, so next time the first value will be the second actual value. When the array is empty, redefine it as to start again.
You can search for more information on array_splice() here.
P.S.: you have to use PHP's $_SESSION to save the array between the pages.
You can use something like $_SESSION and store there the last index.
For example:
$array = array('one', 'two', 'three');
if (!$_SESSION['nextIndex'] || $_SESSION['nextIndex'] >= count($array)) {
$_SESSION['nextIndex'] = 0
}
// print the value
echo $array[$_SESSION['nextIndex']];
// increment the nextIndex
$_SESSION['nextIndex']++;
NOTE: This will only work for the same user. Each page reload will increment the array index. But if you need some cross-user counting, then you have to store the information somewhere on the server, like a DB or even a simple txt file.
Check out this example: http://hibbard.eu/how-to-make-a-simple-visitor-counter-using-php/
Finally I solved this problem with the MySQL. created a column with all code. and then call the script every-time when user press button. In the script first I fetch first raw and print that value, and then, delete that raw. so every-time user will get unique value from the list of code And it is working fine.
I'm trying to create a function in php that will read from an array. I am trying to concatenate text and the array id number, to read the value in the array, however it is the concatenated text that is being returned, not the value of the array.
Here is my code:
//arrays with words in dictionary
$dictionary_word1 = array("test1","test2","test3");
$dictionary_word2 = array("test4","test5","test6");
$dictionary_word3 = array("test7","test8","test9");
$word_to_lookup = "dictionary_word_1";
//value to send to function
$returned_word = convert_word($word_to_lookup);
//value returned from function
echo "<br>the returned word from the function is " . $returned_word;
//the text "$dictionary_word_1[2]" is displayed instead of array value
echo "<br>the value in the array is " . $dictionary_word1[2];
//this displays correcty as "test3"
function convert_word($word_to_convert)
{
global $dictionary_word1;
$converted_word = '$'.$word_to_convert .'[2]';
return $converted_word;
}
Could anyone give me any tips on where I am going wrong?
You need to reference the variable this way to do what you are trying (see PHP: Variable Variables and PHP: Variable Parsing):
$converted_word = ${$word_to_convert}[2];
However, notice the difference between dictionary_word_1 and $dictionary_word1? Won't work.
Regardless, anytime you're doing this you would be better with an array. In this case a multidimensional array. Consider:
$words[1] = array("test1","test2","test3");
$words[2] = array("test4","test5","test6");
$words[3] = array("test7","test8","test9");
Then you're always using $words just changing the index and then using a word from that array.
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>";
}
?>