NATION

PASSWORD

Coding help

Bug reports, general help, ideas for improvements, and questions about how things are meant to work.

Advertisement

Remove ads

User avatar
[violet]
Executive Director
 
Posts: 16205
Founded: Antiquity

Postby [violet] » Sat Dec 20, 2014 2:04 pm

Belique wrote:When I add a shortcut, I use an anchor. However, in one of my dispatches, this doesn't work. The code is correct and I've checked it, and it only works on some of them.

You can't use spaces within an anchor name. So [anchor=RegionalMap] works but not [anchor=Regional Map].

User avatar
Belique
Chargé d'Affaires
 
Posts: 499
Founded: Sep 25, 2013
Ex-Nation

Postby Belique » Sat Dec 20, 2014 2:36 pm

thanks sooo much!
Pro: Christianity, History, Conservatism, Conservationism, Creationists, Tea Party Movement, Manufacturing, Tariffs, Life, Green Energy, Oil, Coal, and the South

Anti: Atheism, Islam, Buddism, Sikhism, Hinduism, Waste of Tax Dollars, Haddron Collider, Welfare, Abortion, Amnesty, and Liberal politics

User avatar
Letoilenoir
Chargé d'Affaires
 
Posts: 424
Founded: Nov 26, 2010
Ex-Nation

Still wrangling with this

Postby Letoilenoir » Sat Mar 28, 2015 2:32 pm

Anyone help with the syntax on this:

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 2</title>
</head>

<body>
<!DOCTYPE html>
<html>
<body>

<?php
$nation_list = array("Letoilenoir","Noirdetoile","Stellanera");

function get_data($nation, $shards = FALSE) {
      if($shards != FALSE) {
         $file = 'http://www.nationstates.net/cgi-bin/api.cgi?nation=' . $nation . '&q=' . $shards;
      }
      else {
         $file = 'http://www.nationstates.net/cgi-bin/api.cgi?nation=' . $nation;
      }
     
      $xml = simplexml_load_file($file);
      return $xml;
   }
   ini_set('user_agent', 'XXXXXXXXXXXXXX');
   function display_strategic_report($nation) {
      $data = get_data($nation, 'name+influence+wa+censusscore-65');
     
      $name = (string) $data->NAME;
      $influence = (string) $data->INFLUENCE;
      $wa = (string) $data->UNSTATUS;
     $censusscore1 = (string) $data->CENSUSSCORE;
     $region = (string) $data->REGION;
   
     echo "<tr><td><a rel='nofollow' target='_blank' href='http://www.nationstates.net/nation=$name'>$name</a>";
     echo "<td>$influence</a>";
    echo "<td>$wa</a>";
    echo "<td>$censusscore1</a>";
    echo "<td><a rel='nofollow' target='_blank' href='http://www.nationstates.net/region=$region'>$region</a>";
       
    }
foreach ($nation_list as $nation)

{
   display_strategic_report;
   
}

?>   

</body>
</html>
</body>

</html>


Basically I'm trying to cycle through the nation_list array and echo the display strategic report function to generate the info for each nation

I know the function works and I can get the "foreach" to generate output but not to execute the function

There is something wrong with the syntax I guess but I'm lost!
:blush: :( :unsure:
KEEP THE BLOOD CAVE FREE

User avatar
The Blaatschapen
Technical Moderator
 
Posts: 63226
Founded: Antiquity
Anarchy

Postby The Blaatschapen » Sat Mar 28, 2015 2:57 pm

Letoilenoir wrote:Anyone help with the syntax on this:

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 2</title>
</head>

<body>
<!DOCTYPE html>
<html>
<body>

<?php
$nation_list = array("Letoilenoir","Noirdetoile","Stellanera");

function get_data($nation, $shards = FALSE) {
      if($shards != FALSE) {
         $file = 'http://www.nationstates.net/cgi-bin/api.cgi?nation=' . $nation . '&q=' . $shards;
      }
      else {
         $file = 'http://www.nationstates.net/cgi-bin/api.cgi?nation=' . $nation;
      }
     
      $xml = simplexml_load_file($file);
      return $xml;
   }
   ini_set('user_agent', 'XXXXXXXXXXXXXX');
   function display_strategic_report($nation) {
      $data = get_data($nation, 'name+influence+wa+censusscore-65');
     
      $name = (string) $data->NAME;
      $influence = (string) $data->INFLUENCE;
      $wa = (string) $data->UNSTATUS;
     $censusscore1 = (string) $data->CENSUSSCORE;
     $region = (string) $data->REGION;
   
     echo "<tr><td><a rel='nofollow' target='_blank' href='http://www.nationstates.net/nation=$name'>$name</a>";
     echo "<td>$influence</a>";
    echo "<td>$wa</a>";
    echo "<td>$censusscore1</a>";
    echo "<td><a rel='nofollow' target='_blank' href='http://www.nationstates.net/region=$region'>$region</a>";
       
    }
foreach ($nation_list as $nation)

{
   display_strategic_report;
   
}

?>   

</body>
</html>
</body>

</html>


Basically I'm trying to cycle through the nation_list array and echo the display strategic report function to generate the info for each nation

I know the function works and I can get the "foreach" to generate output but not to execute the function

There is something wrong with the syntax I guess but I'm lost!
:blush: :( :unsure:


The 'display_strategic_report' call in the last foreach should be
Code: Select all
display_strategic_report($nation);
The Blaatschapen should resign

User avatar
Letoilenoir
Chargé d'Affaires
 
Posts: 424
Founded: Nov 26, 2010
Ex-Nation

Postby Letoilenoir » Sun Mar 29, 2015 1:30 am

The Blaatschapen wrote:
The 'display_strategic_report' call in the last foreach should be
Code: Select all
display_strategic_report($nation);


Many, many thanks The Blaatschapen! :bow:

I also needed to correct the foreach call itself

Code: Select all
foreach ($nation_list as $nation)
{
  display_strategic_report($nation);   
}


Next step is to code user input to determine the array, so watch this space!
KEEP THE BLOOD CAVE FREE

User avatar
The Blaatschapen
Technical Moderator
 
Posts: 63226
Founded: Antiquity
Anarchy

Postby The Blaatschapen » Sun Mar 29, 2015 1:54 am

Letoilenoir wrote:
The Blaatschapen wrote:
The 'display_strategic_report' call in the last foreach should be
Code: Select all
display_strategic_report($nation);


Many, many thanks The Blaatschapen! :bow:

I also needed to correct the foreach call itself

Code: Select all
foreach ($nation_list as $nation)
{
  display_strategic_report($nation);   
}


Next step is to code user input to determine the array, so watch this space!


Btw, can I give some PHP suggestions?

I can recommend not echoing the HTML tags, unless you have to.

Code: Select all
   
         $region = (string) $data->REGION;
       
         echo "<tr><td><a rel='nofollow' target='_blank' href='http://www.nationstates.net/nation=$name'>$name</a>";


Becomes:

Code: Select all
   
         $region = (string) $data->REGION;
       ?>
         <tr>
              <td>
                  <a rel='nofollow' target='_blank' href='http://www.nationstates.net/nation=<?php echo $name;?>'><?php echo $name;?></a>;


This has the benefit that in a nice editor(eg. Notepad++), your HTML syntax is highlighted, rather than being seen as just a string of text. Yes, it means a lot more opening and closing PHP-tags, but the webserver knows hows to nicely handle that :) (I also did some indentation into the HTML, since I'm a perfectionist >_> )

How big will this application be?

(My RL job is PHP developer, so I might have some more tips, but it's also about impact/effort, thus I do not want to burden you with lots of well meant advice, if this remains something simple)
The Blaatschapen should resign

User avatar
Letoilenoir
Chargé d'Affaires
 
Posts: 424
Founded: Nov 26, 2010
Ex-Nation

Postby Letoilenoir » Sun Mar 29, 2015 2:47 am

Prototype here

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Regional Overview</title>
<style type="text/css" media="all">
      @import "http://www.nationstates.net/ns_v129.css";
   </style><!--[if IE 5]>
<style type="text/css">
/* place css box model fixes for IE 5* in this conditional comment */
.twoColFixLt #sidebar1 { width: 230px; }
</style>
<![endif]--><!--[if IE]>
<style type="text/css">
/* place css fixes for all versions of IE in this conditional comment */
.twoColFixLt #sidebar1 { padding-top: 30px; }
.twoColFixLt #mainContent { zoom: 1; }
/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */
</style>
<![endif]--></head>

<body class="twoColFixLt">

<div id="container">
    <div id="mainContent">
    <h1> Regional Overview </h1>
    <h2>(Experimental Dossier)</h2>
    <table><th>Nation<th>Influence<th>WA<th>Score<th>Region
<?php
$nation_list = array("Letoilenoir","Noirdetoile","Stellanera"); /* populate this array with nations you wish to enquire about -max 50 */

   function get_data($nation, $shards = FALSE) {
      if($shards != FALSE) {
         $file = 'http://www.nationstates.net/cgi-bin/api.cgi?nation=' . $nation . '&q=' . $shards;
      }
      else {
         $file = 'http://www.nationstates.net/cgi-bin/api.cgi?nation=' . $nation;
      }
     
      $xml = simplexml_load_file($file);
      return $xml;
   }
   ini_set('user_agent', 'xxxxxxxx');/* replace xxxxxxxx with your main nation name - this allows NS Admin to notify you is your script is causing ploblems */
   function display_strategic_report($nation) {
      $data = get_data($nation, 'name+influence+wa+censusscore-65+region');
     
      $name = (string) $data->NAME;
      $influence = (string) $data->INFLUENCE;
      $wa = (string) $data->UNSTATUS;
     $censusscore1 = (string) $data->CENSUSSCORE;
     $region = (string) $data->REGION;
   
     echo "<tr><td><a rel='nofollow' target='_blank' href='http://www.nationstates.net/nation=$name'>$name</a>";
     echo "<td>$influence</a>";
    echo "<td>$wa</a>";
    echo "<td>$censusscore1</a>";
    echo "<td><a rel='nofollow' target='_blank' href='http://www.nationstates.net/region=$region'>$region</a>";
       
    }/* This function retrives the data  nation and the required shards from the API ready for output*/
   
foreach ($nation_list as $nation)
{
  display_strategic_report($nation);   
}/* this loops through the array and outputs the relevent data (nation + shards) */

?></table>
<br>Many thanks to <a href="http://www.nationstates.net/nation=the_blaatschapen">The Blaatschapen
               </a>for assisting with the gremlins <br>and to <a href="http://www.nationstates.net/nation=glen-rhodes">Glen Rhodes</a>
               for the original code

<!-- end #container --></div>
</body>
</html>


The Blaatschapen wrote:Btw, can I give some PHP suggestions?

I can recommend not echoing the HTML tags, unless you have to.
This has the benefit that in a nice editor(eg. Notepad++), your HTML syntax is highlighted, rather than being seen as just a string of text. Yes, it means a lot more opening and closing PHP-tags, but the webserver knows hows to nicely handle that :) (I also did some indentation into the HTML, since I'm a perfectionist >_> )

How big will this application be?

(My RL job is PHP developer, so I might have some more tips, but it's also about impact/effort, thus I do not want to burden you with lots of well meant advice, if this remains something simple)


I have no objection to any recommendations to improve my efforts - I am under no illusions that my approach can sometimes be a little "Heath Robinson" - I welcome any help that is offered. :) :clap:
Last edited by Letoilenoir on Sun Mar 29, 2015 2:52 am, edited 2 times in total.
KEEP THE BLOOD CAVE FREE

User avatar
Letoilenoir
Chargé d'Affaires
 
Posts: 424
Founded: Nov 26, 2010
Ex-Nation

Postby Letoilenoir » Sat May 23, 2015 7:08 am

Experiment2.php

Code: Select all
 function get_data($nation, $shards = FALSE) {
      if($shards != FALSE) {
         $file = 'http://www.nationstates.net/cgi-bin/api.cgi?nation=' . $nation . '&q=' . $shards;
      }
      else {
         $file = 'http://www.nationstates.net/cgi-bin/api.cgi?nation=' . $nation;
      }
     
      $xml = simplexml_load_file($file);
      return $xml;
   }
   ini_set('user_agent', 'xxxxxxxxxxxx');/* replace xxxxxxxx with your main nation name - this allows NS Admin to notify you is your script is causing ploblems */
   function display_strategic_report($nation) {
      $data = get_data($nation, 'name+influence+wa+censusscore-65+region+lastactivity+lastlogin');
     
      $name = (string) $data->NAME;
      $influence = (string) $data->INFLUENCE;
      $wa = (string) $data->UNSTATUS;
     $censusscore1 = (string) $data->CENSUSSCORE;
     $region = (string) $data->REGION;
     $activity = (string) $data->LASTACTIVITY;
     $timestamp = (string) $data->LASTLOGIN;
   

     echo "<tr><td><a rel='nofollow' target='_blank' href='http://www.nationstates.net/nation=$name'>$name</a>";
     echo "<td>$influence</a>";
    echo "<td>$wa</a>";
    echo "<td>$censusscore1</a>";
    echo "<td><a rel='nofollow' target='_blank' href='http://www.nationstates.net/region=$region'>$region</a>";
    echo "<td>$activity</a>";
    echo date("d-M-y\TH:i:s\Z", $timestamp);// what syntax is used to output this data in the same form as the previous outputs


echo date("d-M-y\TH:i:s\Z", $timestamp);// what syntax is used to output this data in the same form as the previous outputs


Its been a while but I'm wondering if anyone can help with the syntax on this one,
Basically I'm trying to make the timestamp element drawn from the API into a human readable format, and place it in the table as per the other outputs.

The first question is how to put it into a table : putting "<td>("d-M-y\TH:i:s\Z", $timestamp)</a>" throws up syntax error, and although I've tried I cant get the output to position correctly.

The other question relates to the time stamp - it seems to be giving the server time(GMT -7) , and I need to display it in local time - what math calculation do I need to include to rectify this?
KEEP THE BLOOD CAVE FREE

User avatar
The Blaatschapen
Technical Moderator
 
Posts: 63226
Founded: Antiquity
Anarchy

Postby The Blaatschapen » Sat May 23, 2015 7:42 am

Try
Code: Select all
echo "<td>" . date("d-M-y\TH:i:s\Z", $timestamp) . "</td>";


?

For the local time, have you checked http://php.net/manual/en/function.date- ... ne-set.php ?

Or maybe
Code: Select all
$newTimestamp = strtotime("+7 hours", $timestamp);
And then echo the date function with the $newTimestamp.

Also, please close your <td>s with </td> rather than </a>*nitpick* The </a> is only needed in the first echo, because you're having a link there.
The Blaatschapen should resign

User avatar
Letoilenoir
Chargé d'Affaires
 
Posts: 424
Founded: Nov 26, 2010
Ex-Nation

Postby Letoilenoir » Sat May 23, 2015 7:53 am

The Blaatschapen wrote:Try
Code: Select all
echo "<td>" . date("d-M-y\TH:i:s\Z", $timestamp) . "</td>";


?

For the local time, have you checked http://php.net/manual/en/function.date- ... ne-set.php ?

Or maybe
Code: Select all
$newTimestamp = strtotime("+7 hours", $timestamp);
And then echo the date function with the $newTimestamp.

Also, please close your <td>s with </td> rather than </a>*nitpick* The </a> is only needed in the first echo, because you're having a link there.



Cheers B!
Atually stripped in the "gmdate" function, just need to compensate for DST now!
Code: Select all
 echo "<td>" . gmdate("d-M-y\TH:i:s\Z", $timestamp) . "</td>";
KEEP THE BLOOD CAVE FREE

User avatar
Letoilenoir
Chargé d'Affaires
 
Posts: 424
Founded: Nov 26, 2010
Ex-Nation

Postby Letoilenoir » Mon May 25, 2015 1:53 am

Next task:
User input of region name makes a call to the API eg:
Code: Select all
http://www.nationstates.net/cgi-bin/api.cgi?region=the_blood_cave&q=numnations+nations


If the number of nations exceeds 50 an error message is displayed, else the script parses the resulting list of nations and populates the nations array
KEEP THE BLOOD CAVE FREE

Previous

Advertisement

Remove ads

Return to Technical

Who is online

Users browsing this forum: Bagong Timog Mindanao, Bisofeyr, Danternoust, Ezaekia, Glory Be Ferret, Liberated Panem, Luziyca, Majestic-12 [Bot], Shirahime, The Hanzanburg Union, Tungstan

Advertisement

Remove ads