I want to get the value from a loop function but am stuck in this problem, maybe you guys can give me help, thanks.
I define the parameters here
$my_value1 = "Order";
$my_value2 = "Transaction";
$my_value3 = "Name";
$doc2 = new DOMDocument();
$doc2->load( 'GetOrders.xml' );
$info2 = $doc2->getElementsByTagName( $my_value1 );
foreach( $info2 as $Type2 )
{
$ebayOrder = $Type2->getElementsByTagName($my_value2);
foreach($ebayOrder as $Type3)
{
echo getMyValue("OrderLineItemID",$Type3); echo '</br>';
}
The problem is that I want to call something like set the my_value2 in the loop and call something like
getMyValue($my_value3,$Type3)
Is possible to rewrite the function, so I can always call using the new parameter?
thanks in advance,
Mike
You can use variables as a parts of the names of other variables. Try with:
for ($i = 1; $i <= 3; $i++) {
$my_value = ${'my_value' . $i};
$info2 = $doc2->getElementsByTagName( $my_value );
}
Related
i'm trying with no success to iterate throug an xml string. I did it before and it worked fine.
I get this from a SOAP server doing this:
$lr = $client->__doRequest($request, $url, $action, $version);
dd($lr);
I get this:
Too long to be here. It's on pastebin
When i try this:
$xml = new \DOMDocument();
$xml->loadXML($lr);
$i = 0;
while(is_object($productos = $xml->getElementsByTagName('Productos')->item($i))) {
foreach($productos->childNodes as $nodename) {
echo $i.": ".$nodename->nodeName." - ".$nodename->nodeValue."<br>";
}
$i++;
}
I get this: Result with products
Now, i want to do something like this:
foreach ($productos as $producto) {
$referencia_dist = trim($producto->IdProducto);
$mpn = trim($producto->PartNumber);
$ean = trim($producto->UpcCode);
and son on....
}
But i get stacked here. Any hints???
OK! Got the solution! It is easier than i thought... Perhaps, that's the reason why i didn't find it...
while(is_object($productos = $xml->getElementsByTagName('Productos')->item($i))){
$nombre = "";
$IdCategoria = "";
foreach($productos->childNodes as $producto)
{
switch ($producto->nodename){
case 'IdProducto':
$IdProducto = $producto->nodeValue;
case 'IdTarifa':
$IdTarifa = $producto->nodeValue;
case 'MostrarComparadores':
$MostrarComparadores = $producto->nodeValue;
case....
}
I'm trying to build a re-usable function to multidimansional array for every tablename i put in.
Like most i start mine tables whit ID and want to start the array whit that id, so not start whit 0 :) . foneticly--> $my_array[id]["email"]
So to build it i thought "what do i need" and found : $count_colum, $total_row an ofcource the data itself.
I know how to build a array, but i dont know how to build a "variable" array.
I also know i cant use PHP inside a array :) (whish would help iff you tell me)
$my_array = array( $row['id'] for ($i = 0; $i < $count_colum; $i++){...}
I also hope somebody knows what i mean :) I'm a little bit new to this all ;)
This is what i get this far:
function make_MDA($tablename)
{
$dbh = new PDO("mysql:host='localhost';dbname='dbname', 'usr','pas'");
$query = $dbh->prepare("SELECT * FROM `".$tablename."`");
$query->execute();
$count_colum = $query->columnCount();
$result = $query->fetchAll();
$total_row = count($result);
for ($i = 0; $i < $count_colum; $i++)
{
$meta = $query->getColumnMeta($i);
$column[] = $meta['name'];
}
foreach ($result as $row)
{
$my_array = array( $row['id'] //this is where I'm stuck
}
// echo $column[3];
// echo $total_row;
// echo $count_colum;
}
This should work, don't overcomplicate things:
function make_MDA($tablename)
{
$dbh = new PDO("mysql:host='localhost';dbname='dbname', 'usr','pas'");
$query = $dbh->prepare("SELECT * FROM `".$tablename."`");
$query->execute();
$result = $query->fetchAll();
foreach ($result as $row)
{
$id = $row['id'];
$my_array[$id] = $row;
}
return $my_array;
}
I would make the connection to the database $dbh once outside this function.
I have the following code which does not seem to be working. As far as i can tell the predefined arrays are each of the same size - i have 32 rows in my table "homepage" where "username", "image" and "website" are three fields. For testing purposes the usernames are 1 to 32. And the image and website fields are blank - at the moment (this will change).
The code i have is:
$sorthpge = mysql_query ("SELECT * FROM homepage ORDER BY no DESC LIMIT 32");
$links = array();
$images = array();
$usern = array();
$array_Length_1 = count($usern);
for ($i=0; $i<$array_Length_1; $i++)
{
while ($row_1 = mysql_fetch_assoc ($sorthpge)) {
$images[$i] = $row_1['image'];
$links[$i] = $row_1['website'];
$usern[$i] = $row_1['username'];
if($images[$i] == ""){
$images[$i] = "uploads/default.png";
$links[$i] = "register.php?no=";
}
else
{
if($images[$i] == "auction"){
$images [$i] = "uploads/auction.png";
$links[$i] = "auction.php?no=";
}
}
}
}
You can probably tell what i'm trying to do. As mentioned all the "image" rows are blank so i should be getting "$images[i] = "uploads/default.png" for all i up to 32. But nothing is showing in my html.
Just wondered if somebody could point out an error in my code. Or if my set up assumes something wrong. I'm pretty new to php. Thanks a lot in advance. P.S i will translate to mysqli when i can get these basics working.
If this is all the code, this is the problem:
$usern = array();
$array_Length_1 = count($usern);
for ($i=0; $i<$array_Length_1; $i++)
{
The array is empty, so the length is 0 so your code will never run.
What is the purpose of that for loop anyway? It seems you can just remove it.
In your sample code, you have the following:
$usern = array();
$array_Length_1 = count($usern);
for ($i=0; $i<$array_Length_1; $i++) {
As $usern is empty, $array_Length_1 is 0 - your loop is never executed.
I'm not sure what your logic behind doing this was/is, so I don't know a proper way to suggest to fix it, however, if you were to remove the for loop entirely and store a separate incrementer, $i, the code should work fine.
For instance, try updating your code to the following:
$sorthpge = mysql_query ("SELECT * FROM homepage ORDER BY no DESC LIMIT 32");
$links = array();
$images = array();
$usern = array();
$i = 0;
while ($row_1 = mysql_fetch_assoc ($sorthpge)) {
$images[$i] = $row_1['image'];
$links[$i] = $row_1['website'];
$usern[$i] = $row_1['username'];
if($images[$i] == ""){
$images[$i] = "uploads/default.png";
$links[$i] = "register.php?no=";
} else if($images[$i] == "auction"){
$images [$i] = "uploads/auction.png";
$links[$i] = "auction.php?no=";
}
$i++;
}
$array_Length_1 = count($usern);
Is you problem ! Array is empty since you just declared it
One problem is that $array_Length_1 = count($usern); Your array $usern is empty.And you for loop max value is empty. meaning that $i is incrementing to 0.
$usern = array();
$array_Length_1 = count($usern);
for ($i=0; $i<$array_Length_1; $i++)
And make sure your file is saved as a php file as you mentioned your html file is showing nothing.
Your above code was failing in a few places I think. This $array_Length_1 = count($usern); wasn't helping. The for loop was running off the count of the $array_length, which was always going to be empty because it hadn't been filled yet...
Give this a try:
<?php
// connection strings here or db include
$sorthpge = mysql_query ("SELECT * FROM homepage ORDER BY no DESC");
$links = array();
$images = array();
$usern = array();
$array_Length_1 = count($usern);
$i = 0;
while ($row_1 = mysql_fetch_assoc($sorthpge)){
$images[$i] = $row_1['image'];
$links[$i] = $row_1['website'];
$usern[$i] = $row_1['username'];
if($row_1['image'] == ""){
$images[$i] = "uploads/default.png";
$links[$i] = "register.php?no=";
}
elseif($row_1['image'] == "auction"){
$images [$i] = "uploads/auction.png";
$links[$i] = "auction.php?no=";
}
$i++;
}
?>
I have dateValues as follows,
[dateVals] = 031012,041012;
it is comma seperated values. I want to make this as array and to get individual values . As i am new to PHP , i want some one's help .
$val = array[dataVals];
for($i=0;$i<sizeof($val);$i++) {
echo "val is".$val[$i]."\n";
}
is not working
use this code
$dateVals = '031012,041012';
$pieces = explode(",", $dateVals);
for($i=0;$i<sizeof($pieces);$i++) {
echo "val is".$pieces[$i]."\n";
}
it will give you proper output.
working example http://codepad.viper-7.com/PQBiZ3
$dateVals = '031012,041012';
$dateValsArr = explode(',', $dateVals);
foreach( $dateValsArr as $date) {
}
Try this code.
Try this One
$dateVals = '031012,041012';
$date_arr[] = explode(',',$dateVals);
for($i = 0 ; $i < count($date_arr) ; $i++)
{
print $date_arr[$i].'<br />';
}
Need to store values from foreach loop into an array, need help doing that.
The code below does not work, only stores the last value, tried $items .= ..., but that is not doing the trick either, any help will be appreciated.
foreach($group_membership as $i => $username) {
$items = array($username);
}
print_r($items);
Declare the $items array outside the loop and use $items[] to add items to the array:
$items = array();
foreach($group_membership as $username) {
$items[] = $username;
}
print_r($items);
Use
$items[] = $username;
Try
$items = array_values ( $group_membership );
<?php
$items = array();
$count = 0;
foreach($group_membership as $i => $username) {
$items[$count++] = $username;
}
print_r($items);
?>
You can try to do my answer,
you wrote this:
<?php
foreach($group_membership as $i => $username) {
$items = array($username);
}
print_r($items);
?>
And in your case I would do this:
<?php
$items = array();
foreach ($group_membership as $username) { // If you need the pointer (but I don't think) you have to add '$i => ' before $username
$items[] = $username;
} ?>
As you show in your question it seems that you need an array of usernames that are in a particular group :) In this case I prefer a good sql query with a simple while loop ;)
<?php
$query = "SELECT `username` FROM group_membership AS gm LEFT JOIN users AS u ON gm.`idUser` = u.`idUser`";
$result = mysql_query($query);
while ($record = mysql_fetch_array($result)) { \
$items[] = $username;
}
?>
while is faster, but the last example is only a result of an observation. :)
This is how you do it:
foreach($orders as $item){
$transactionIds[] = $item->generated_order_id;
}
dump($transactionIds);
This is assuming $orders collection has a field called 'generated_order_id'.
$items=array();
$j=0;
foreach($group_membership as $i => $username){
$items[$j++]=$username;
}
Just try the above in your code .
Just to save you too much typos:
foreach($group_membership as $username){
$username->items = array(additional array to add);
}
print_r($group_membership);
this question seem quite old but incase you come pass it, you can use the PHP inbuilt function array_push() to push data in an array using the example below.
<?php
$item = array();
foreach($group_membership as $i => $username) {
array_push($item, $username);
}
print_r($items);
?>