This seemed simply but i'm running into a problem. I'm building an autocomplete drop down box using jquery ui. It works fine with static information but when i try put dynamic data from mysql in the array it freaks out.
Parse error: syntax error, unexpected T_DO, expecting ')' on line 46
Line 46 is the do in the array
$items = array(do {
$row_Recordset1['ARTIST'];
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); );
I've searched the net but I can't find anything relating to problems with a do in an array.
Sorry if this is really stupid but I can't seem to get it to work
Thanks
You can't execute code inside an array statement. Please read the manual for the array method. It excepts values , not code.
You should initialise your array , and then use your do/while loop to populate data.
$items = array();
do{
$items[] = $row['artist'];
}while('condition');
Your code should be like below, even should not use do ... while loop.
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {
$items[] = $row_Recordset1 ['ARTIST'];
}
Related
I'm trying to create an associative array in PHP using an Object. The Object is values from a database, called Category. It only has two values, an id and a name field.
This is what I have:
$category = $em->getRepository('AppBundle:Category')->findAll();
$stuff = array();
foreach($category as $cat) {
$stuff[$cat->getName()] = $stuff[$cat->getId()];
}
But I get this nasty error:
Notice: Undefined offset: 1
I should say that I am using Symfony 3. Any help would be great.
You're on the right track but you need to remove echo since you're not trying to output anything. You're also attempting to assign a value from the array you're trying to enter a value into.
$stuff[ $cat->getName() ] = $cat->getId();
Sorry I just omitted the value and replace with $cat->getId() in stead of $stuff[$cat->getId()] and it worked
You $stuff array has no index 1, so it will cause that issue.
This is caused by $stuff[$cat->getId()];, which should be $cat->getId();
foreach($category as $cat) {
$stuff[$cat->getName()] = $cat->getId();
}
I've been trying for a while. I have tried several things to fix this, but I just can't get it to work.
My code:
<?php
if (is_array($row))
{
foreach ($row as $data) {
echo array_unique($data->username);
}
}
?>
It gives me the following error
Message: array_unique() expects parameter 1 to be array, string given
I have no idea what is going on with this. I have even tried placing the array_unique in the $row.
So like:
<?php
if (is_array($row))
{
foreach (array_unique($row) as $data) {
echo $data->username;
}
}
?>
But this gives me another error:
Object of class stdClass could not be converted to string
I have no idea what's going on. I have searched for hours but haven't found anything on here. Any help is greatly appreciated. Thanks.
You can't use array_unique on multi-dimensional arrays when you're looking inside the depth. Its works on flat one, and certainly won't work on strings. An alternative is to create another container for that and use usernames as keys, then you'll get unique ones.
Since you haven't shown the array/object structure, here a little bit on an idea on the comment I gave above:
$container = array();
foreach($row as $data) {
if(!isset($container[$data->username])) {
$container[$data->username] = $data;
}
}
// $container = array_values($container); // optional simple reindex
I have a string stored in a database that I'm pulling into an array lets say for example the string is "$foo" what I'm trying to do is use that string as a php variable to be interpreted by php, but I can't seem to figure it out. I did try using eval, but I couldn't get that to work in my current code and from what I've read eval() is too dangerous to use in a live environment.
Here is a sample of my code
$result = $sql->query("SELECT * FROM newordersystem");
for ($set = array (); $row = $result->fetch_assoc(); $set[] = $row);
foreach ($set as $item) {
$item[PHPVARIABLE] = stripslashes($_POST[$item[INPUTIDNAME]]);}
The last line is where my problem lies, in that I want "$item[PHPVARIABLE]" to execute as $foo so the result would end up being $foo = 1
Any help or advice would be greatly appreciated!
Strange architecture and I don't get 100% your goal, but if you need:
$item[$item[INPUTIDNAME]] = stripslashes($_POST[$item[INPUTIDNAME]]);}
or
${$item[PHPVARIABLE]} = stripslashes($_POST[$item[INPUTIDNAME]]);}
On a previous page, I have gotten the names and the number of playeys values from the user.
I wish to fill them into an array. Here is my code so far:
<?php
$numberOfPlayers = $_POST['numberOfPlayers'];
$counter = 1;
$playerName = array();
while($counter<=$numberOfPlayers-1){
$playerName[$counter-1]= $_POST[$counter];
$counter=$counter+1;
}
print_r($playerName);
?>
However it is throwing out an error saying "Undefined offset: 1 in C:\wamp\www\test.php on line 8".
Any help would be much appreciated.
Thanks
Just use array_push(). or [] array short hand. No need to use a counter.
while($numberOfPlayers--){
if(isset($_POST[$numberOfPlayers]))
$playerName[]= $_POST[$numberOfPlayers];
}
Why won't this work?
$slidetotal=1;
$slideids = array();
while ($rowcs = mysql_fetch_array($orig_slides_result)) {
$key = $slidetotal;
array_push($slideids[$key], $rowcs['id']);
$slidetotal++;
}
I get this error:
[phpBB Debug] PHP Notice: in file ///*.php on line 161: array_push() [function.array-push]: First argument should be an array
Although someone has commented you can do this on this page:
http://php.net/manual/en/function.array-push.php , (find: "to insert a "$key" => "$value" pair into an array")
What is the next best way to insert a list of single values into a php array? By the way, I really can't believe it's hard to find something on this with google.com. Seriously?
That PHP.net comment is incorrect. That is pushing $rowcs['id'] onto the array $slideids[$key], not the array $slideids.
You should be doing the following, in place of your array_push() call:
$slideids[$key] = $rowcs['id'];
Why don't you do;
$slidetotal=1;
$slideids = array();
while ($rowcs = mysql_fetch_array($orig_slides_result)) {
$slideids[$slidetotal] = $rowcs['id'];
$slidetotal++;
}
Also you can do like below if you don't need the key to start from 1;
$slideids = array();
while ($rowcs = mysql_fetch_array($orig_slides_result)) {
$slideids[] = $rowcs['id'];
}
ummm hard-searching will work for google I think :)
anyway, error tells you everything you need to know. that means first argument of array_push is not an array, you give a single value (string) to array_push ($slideids[$key]).
Also why do you need to use array_push in php? I'd rather use
$slideids[] = $rowcs['id'];
and what you're trying to do is:
$slideids[$key] = $rowcs['id'];
i guess...