Json Decode,Access, show and separate names with ID, every 9 names - php

Hello :) I give you the Json Decode below.
http://pastebin.com/XqhTMWCS
It presents my last 10 games. I need to get my 4 teamates for every game,and my 5 enemies.(names) When someone is a teamate, his "teamId" is 100 and when someone is an enemy,the "teamId" is 200. I need to separate and show them.
Like: You were playing Vayne. Your team was:
sirjack1101 playing champion ID 89
blablaa playing ID 22
blablaaa playing ID 23
blablaaaa playing ID 24
The enemy team was:
blablsaa playing ID 31
bsadaaas playing ID 12
basdasdb playing ID 53
blablsad playing ID 67
blablsav playing ID 121
I can find only the first username using this :
$recent = "My json" ;
$rec = file_get_contents($recent);
$recdat = json_decode($rec, true);
$fellow = $recdat[gameStatistics][0][fellowPlayers][0][summonerName];
echo $fellow;
And I can get only my 1st champion name using the same code... I need your help! :)
Thanks in advance!

Sounds like you just need a foreach loop to iterate over all players.
<?php
define('TEAMMATE_ID', 100);
define('ENEMY_ID', 200);
$teammates = $enemies = array();
foreach ($data['gameStatistics'][0]['fellowPlayers'] as $player) {
if ($player['teamId'] == TEAMMATE_ID) {
$teammates[] = $player['summonerName'] .' playing ID '. $player['championId'];
}
else if ($player['teamId'] == ENEMY_ID) {
$enemies[] = $player['summonerName'] .' playing ID '. $player['championId'];
}
}
// List of teammates.
print "Teammates:\n";
print implode("\n", array_slice($teammates, 0, 4));
// List of enemies.
print "Enemies:\n";
print implode("\n", array_slice($enemies, 0, 5));
?>
Hopefully I'm not way off base...

Related

How to count JSON response and output how many lines there are

Hello fellow developers,
I have been trying to manipulate the output and display the total amount of workers there are instead of outputting the workers name as a string.
Bellow you will find the data that i am receiving and further down i will explain how i would like to handle the JSON response.
{
"result":
{
"addr":"ADDRESS_HERE",
"workers":
[
["worker1080",{},2,1,"200000",0,22],
["worker1080",{"a":"899.4"},3,1,"512",0,24]
],
"algo":-1
},
"method":"stats.provider.workers"
}
So basically as you can see from the above response that there are 2 workers named "worker1080" active on that address.
The bellow php code is how i retrieve the data and output only the names of the workers:
<?php
$btcwallet = get_btc_addy();
if (isset($cur_addy)) {
$method4 = new methods();
$worker_stats = new urls();
$get_data = file_get_contents(utf8_encode($worker_stats->nice_url.$method4->m4.$cur_addy));
$get_json = json_decode($get_data, true);
foreach ($get_json['result']['workers'] as $v) {
$i = 0;
print $v[$i++]."<br />";
}
}
?>
$get_json is the variable that decodes the data from $get_data and displays the worker names and increments every time a worker is added or online.
now i currently have 2 workers online as shown in the JSON response.
it outputs:
worker1080
worker1080
which is perfect although if i try using a foreach statement and try to display the the total amount of workers online it should display 2 instead of the names, it has to also increment for each worker that the json repsonse outputs.
EG: i have 2 workers online now, but in an hour i will connect 10 more it would display the following:
worker1080
worker1080
worker1070
worker1070
worker1080ti
worker1080ti
workerASIC
workerASIC
workerASIC
workerCPU
workerCPU
workerCPU
Now i try to use the following to display the total:
count($v[$i++]);
and i have tried using a foreach within the foreach, and both count and the foreach both will either display "0" by all the workers or "1"
bellow is an example of the output.
0
0
0
0
0
How would i go about counting each line in the output and display the total number of workers ?
Thanks #symcbean for the solution.
<?php
$btcwallet = get_btc_addy();
if (isset($cur_addy)) {
$method4 = new methods();
$worker_stats = new urls();
$get_data = file_get_contents(utf8_encode($worker_stats->nice_url.$method4->m4.$cur_addy));
$get_json = json_decode($get_data, true);
print count($get_json['result']['workers'])."<br />"; // <-- solution *removed foreach and $i incrementation as its not needed for count
}
?>
it now displays the correct number of workers :)

While loop for TCPDF

I have problem to loop the data in TCPDF..The output should be display like this
Output :
Chair x 3
Table x 5
LCD X 2
but output now just show : Chair x 3
It only display the first record. Can anyone explain
how to loop the data based on output above?
$result_1=mysql_query($query_1);
while($row1=mysql_fetch_array($result_1)){
$item = $row1['item'];
$qty = $row1['SUM(qty)'];
$data = $item.' x '.$qty;
$tbl.='<td align="center">'.$data.'</td>';
$data='';
}
$tbl.='</tr>';
}
This picture output I get based on coding above
This is answer for my question. I just add $data. at $data = $item.' x '.$qty; and my problem has been solved.
$result_1=mysql_query($query_1);
while($row1=mysql_fetch_array($result_1)){
$item = $row1['item'];
$qty = $row1['SUM(qty)'];
$data = $data.$item.' x '.$qty.'<br>';
}
$tbl.='<td align="center">'.$data.'</td>';
$data='';
$tbl.='</tr>';
}

PHP: "While" loop returns same value for each iteration of the variable

Thanks in advance for any help offered.
I'm performing a query that returns something like this:
route | busnumber
2 300
4 123
2 455
12 934
My goal is to print/echo a list of all "busnumber" were "route" = 2.
I know I can do a query on the DB to do this but I don't want to run this query for each and every route. The result set is actually part of one master, complex query so I need to accomplish my goal using the array.
The query works correctly and does display the appropriate info. I also return the results into an array. i.e ...
$query=("SELECT * from foo")
$result=mysql_fetch_assoc($query);
My current code looks like this (note my comments):
mysql_data_seek( $result,0); // because I'm previously iterating through $result
while ($i2=mysql_fetch_assoc($result)) {
$busnumber=$i2['busnumber'];
$busroute=$i2['route'];
echo "<div class='businfo'>";
if ($busroute='2'){ // I only want this to happen if $busroute=2
echo "Current Route = $busroute</br>";
echo "Bus Number : $busnumber </br>";
}
echo "</div>";
}
What I'm getting, however, it is echoing the same '$busroute' (2) but different '$busnumber' for each row as such:
Current Route = 2
Bus Number : 194
Current Route = 2
Bus Number : 196
Current Route = 2
Bus Number : 2002
Current Route = 2
Bus Number : 2010
Current Route = 2
Bus Number : 2013
The problem is that all of those bus numbers are not part of route 2. It is a listing of bus numbers from all routes. I only want it to perform the foreach loop if $routenumber=2.
Thanks again for the help :)
if ($busroute='2'){
The problem is that you're not checking whether it's equal to 2 here, you're assigning it to equal 2. Change it to this and you'll be fine:
if ($busroute=='2'){
$query = "SELECT * from foo";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
if ($row['route'] == 2) {
// do your stuff
}
}

How to retrieve 1 record at a time from db

I have a table comment as follow:
Comment
comment_id cmt followupid
1 Hello 3
2 hi 4
3 Hey 2
4 wassup 1
My query is that I want to echo "Hello", "hi", "hey" , "Wassup" and other (the record continues) individualy, I have used
$comment = mysql_result($runQuery, $i,"cmt");
echo $comment;
which works fine but the problem is that it echoes all the comments at once, what I want is to echo all the comment but one at a time. the comment are in a div tag such that each the div appears only after 1 second the page is loaded. I want each comment to appear after different time interval
for e.g:
Hello to appear at 5pm (not necessarily the corect time it can be just an int)
hi 5.10 pm
hey 6.30 pm
Please Help!
The following code should give you some hints.
$result = mysql_query($runquery);
while($row=mysql_fetch_assoc($result)){
// $row contains a single row.
echo $row['cmt'], $row['comment_id']
}
Create another variable storing time divisions(or number of rows). So that different output at different time can be fetched. For eg. If your table has 24 rows(or items), then this variable shall have a value 24. Use it to divide you output times(As in 24 hours, each hour a different value).
Now, the PHP part(I am not much familiar with date and time functions in PHP, so you can totally ignore the paragraph above.
$result = mysql_query($runquery);
$j = 0;
$i = rand( 0, mysql_num_rows($result) );
while($row=mysql_fetch_assoc($result)){
// $row contains a single row.
if( $j++ = $i )
echo $row['cmt'], $row['comment_id'];
}
This will fetch one random row from the table, but not depending upon the server time.

MySql Query adding number 1 to variable when used in query?

Hope you can help I have a simple query updating positions x and y based various user id etc. But I have a problem when I pass the variable to be updated (through ajax) to PHP, I get the variable fine but on placing it in a query a number 1 is added to the query end making the last id unusable (see example id 68 becomes 681).
Never seen this before, I am relatively new to sql tho, hope someone can shed some light on this?
$xupdate = $_POST['xupdate'];
$yupdate = $_POST['yupdate'];
$stickytext_id = $_POST['stickytextid'];
$user_id= $_POST['uid'];
$proj_id=$_POST['projid'];
echo $xupdate; //output 358
echo'<br>';
echo $yupdate; //output 203
echo'<br>';
echo $stickytext_id; //output 68
echo'<br>';
echo $proj_id; //output 7
echo'<br>';
$sql_update_stickyxy="UPDATE textsticky SET textsticky_x = $xupdate AND textsticky_y = $yupdate
WHERE textsticky_id = $stickytext_id";
echo $sql_update_stickyxy; //outputs UPDATE textsticky SET textsticky_x = 358 WHERE textsticky_id = 681 not 68?
Looking at your echo'd output you obviously embezzled some of your code. As a first debugging measure you might use $_POST['stickytextid'] instead of $stickytext_id inside your query and see where it gets you.

Categories