Errors using new array functions PHP [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hello All: I do not have much experience with Arrays but am trying to learn. Trying something new and don't know if this can be done this way or not. Essentially what I am trying to do is go through a wp database table; and go through the records; the $postnum value is equivalent to a WP post_id and is in the database multiple times. So I want to just whip through the table and create an array with each post_id ($postnum) in there just once. So I thought, all i need to do is create an array outside of loop; then while in loop just check if the $postnum is already in the array; if it is not just add it…so then all records with that same postnum in them they will not add to the array…
here is my code:
$posts_counted=array();
global $wpdb;
foreach( $wpdb->get_results("SELECT * FROM wp_c93hh3bk23_pvc_daily") as $key => $row) {
$id = $row->id;
$time = $row->time;
$postnum = $row->postnum;
$postcount = $row->postcount;
if (in_array($postnum,$posts_counted)) 
{
array_push($posts_counted,$postnum);
}
} // close out foreach
I have never really used the "in_array" thing or the "array_push" thing either; so these are all new to me…
I am getting: "Parse error: syntax error, unexpected ' ' (T_STRING) in…"
referring to the line "if (in_array($postnum,$posts_counted))"
So I am wondering if anyone can tell me if what I am trying to do can be done this way or what I am doing wrong on this…
Thanks!

This is how I get data from db:
$sql= "Select * FROM table";
$result=mysql_query($sql);
while($rows = mysql_fetchArray($result)){
$data= $rows;
}
//checking if something is in array
if(in_array($data['id')){
//do something
}
//now how you want to get data
// echo json_encode($data)
//OUTPUT: {id:"1",name:"xyz"}
Anyway if you want your "postNum" unique, fix your sql with adding DISTINCT after SELECT.

Related

php: why is $row[0] not returning anything? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 13 days ago.
Improve this question
I am converting all my php scripts due to moving to a new server. I am stumped as to why $row[0] is not working.
I am correctly getting $row populated as an array, and if I run a foreach on it, I get all the values populated just fine. But if, instead, I try to directly access the first value of the array as $row[0], I get nothing. Anyone know what?
$sql = "DESCRIBE USER";
$result = $dbh->query($sql);
$count=0;
while($row = $result->fetch_assoc()) {
print $row[0]; // this prints nothing
foreach($row as $column) {
print "$column"; // this works as expected
}
} #<-- while
fetch_assoc returns your results as a key value of column names and values, so you need to look at the $row['myColumn'] to get the value.

How do I get individual data from an API array in PHP? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to write a little webapp that will show some data. The data comes from an API. All data from the API I get displayed, but if I only want to display specific data, I always get an error.
I have not had much experience with API's yet, but all the tutorials I've done show the data via this echo method.
My current code:
<?php
$user_id = "3bb8d3bc-ab7a-45fb-8154-ed54897b2c4c";
$api_response = file_get_contents('https://r6tab.com/api/player.php?p_id='.$user_id);
$api_response_decoded = json_decode($api_response, true);
//foreach($api_response_decoded as $val) {
//echo $val, '<br>';
//}
echo $api_response_decoded->p_name;
?>
api_response Output:
"playerfound":true,"social":{"twitter":"","instagram":"","mixer":"","twitch":"","youtube":"","bio":"","esl":"","discord":"","background":"","aliases":"","embed":""},"seasonal":{"current_NA_mmr":0,"last_NA_mmr":0,"last_NA_mmrchange":0,"current_EU_mmr":3831,"last_EU_mmr":3881,"last_EU_mmrchange":-50,"current_AS_mmr":0,"last_AS_mmr":0,"last_AS_mmrchange":0,"total_casualwins":6,"total_casuallosses":5,"total_casualtotal":11,"total_casualkills":28,"total_casualdeaths":18,"total_rankedwins":38,"total_rankedlosses":24,"total_rankedtotal":62,"total_rankedkills":244,"total_rankeddeaths":201,"total_generalwins":44,"total_generallosses":29,"total_generaltotal":73,"total_generalkills":272,"total_generaldeaths":219,"total_totalbulletshits":3851,"total_totalhs":172,"total_totaltimeplayed":0,"bomb_wins":67,"bomb_losses":41,"bomb_total":108,"secure_wins":0,"secure_losses":0,"secure_total":0,"hostage_wins":1,"hostage_losses":0,"hostage_total":1,"favorite_mode":"bomb"},"matches":[{"casual_wlstatus":"won","casual_winslost":"1 Won","casual_datatime":"10\/02\/19","ranked_wlstatus":"won","ranked_winslost":"6 Won, 2 Lost","ranked_datatime":"10\/02\/19","next":"default","db_p_total_casualwins":1,"db_p_total_casuallosses":0,"db_p_total_casualkills":2,"db_p_total_casualdeaths":2,"db_p_total_rankedwins":6,"db_p_total_rankedlosses":2,"db_p_total_rankedkills":23,"db_p_total_rankeddeaths":23,"db_p_total_totalhs":10,"db_p_NA_currentmmr":0,"db_p_EU_currentmmr":3881,"db_p_AS_currentmmr":0,"NA_mmrchange":0,"EU_mmrchange":189,"AS_mmrchange":0},
My Error Message:
Trying to get property 'p_name' of non-object in C:\xampp\htdocs\R6S-Stats\stats.php on line 12
By using the true flag on json_decode, you're creating an associated array instead of an object. Remove that flag, and you'll be able to use the object methods:
$api_response_decoded = json_decode($api_response);
echo $api_response_decoded->p_name; //Scenus
If you want to keep it as an associated array, you can access the data like this:
echo $api_response_decoded['p_name'];
when use json_decode('dsfsdf', true);
$p_name = $api_response_decoded['p_name'];
when use json_decode('dsfsdf);
$p_name = $api_response_decoded->p_name;

How to run multiple functions inside a foreach loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I would like to run a foreach loop with three functions inside of it but only the first function runs.
Here is my code
function import_events() {
global $wpdb;
$query_name = $wpdb->prefix . 'options';
$options = $wpdb->get_results("SELECT option_name, option_value FROM $query_name WHERE option_name LIKE 'event_url%'");
// array of option names
$i=1;
foreach ($options as $key => $row) {
$url = $row->option_value;
$city_name = $row->option_name;
import_venues($url);
import_organizers($url);
import_events_calendar($url);
}
echo"The Import Is Finished";
wp_die();
}
Any help is appreciated!
Based on your limited information there may be several possible causes to why the two remaining functions aren't running.
#1:
If your code outputs the "The Import Is Finished", then your functions are simply not doing what you ment for them to do. Perform more debug inside those functions.
#2:
If your code stops running at some point, it has probably stopped working due to a fatal error. Find the file named "error_log" (no extension) and see the entry with the most recent timestamp to determine if it may explain why your code stopped working.
#3:
Perhaps you're simply not seeing any errors. Since you're obviously working with WordPress, the error_log should end up in either /error_log or in /wp-admin/error_log.
While debugging, make sure to edit your /wp-config.php and define('WP_DEBUG', true) instead of false to enable error-logging.
If you're wanting to run functions you have in the same page which I'm assuming are due to the names you are using use these 3 lines to execute them within the for loop
$this->import_venues($url);
$this->import_organizers($url);
$this->import_events_calendar($url);
The problem ended up being in the function import_events_calendar($url)
The function was not returning anything which looked like it was not running but it really was just returning nothing.
Thanks for all the help!

How to get a value from nested array? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Trying to fetch the first URL field from an array of them that comes from a JSON I have decoded but I get this error:
Parse error: syntax error, unexpected '[' in C:\blabla
foreach($data-> images as $data2) {
print_r(images[0]['url']);
}
I hope its enough of my code to work out what I am doing wrong?
Added: I would like the first "url" and it was getting the last one hence why I am changing the code and trying to debug it here.
Within your foreach you use the variable name you specified in the definition:
So something like...
foreach($data->images as $data2) {
print_r($data2[0]['url']);
}
Although, depending on the structure of the array, I'd imagine that you don't need the number, so it might be:
foreach($data->images as $data2) {
print_r($data2['url']);
}
If you wanted to loop through the values by a number, you'd use a for loop
for ($i = 0; $i <= count($data->images); $i++)
{
print_r($data->images[$i]);
}

PHP array only showing last sql result [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hello i am hoping someone can help me, when i use print_r all only get the last result from the mysqli query, my code is below.
//Fetch data from sql results
while($row = $rs->fetch_assoc()){
//Put results in a array
$page_query=array($row['name']=>$row['system']);
}
}
you are overwriting your $page_query everytime in loop, change to:
while($row = $rs->fetch_assoc()){
//Put results in a array
$page_query[] =array($row['name']=>$row['system']);
}
You need to add it to the array - not replace the entire variable with what you have in that row.
while($row = $rs->fetch_assoc())
{
//Put results in a array
$page_query[]=array($row['name']=>$row['system']);
}
The short syntax for the function you are looking Array_push is simply to pop a set of empty square brackets behind the variable and then say =something;. This appends another element to the end of the array. This function will increment the index numerically.
because every time inside while you reinitialize $page_query so you should push them inside array to collect. use array_push()

Categories