駅データ.jpの中の路線APIを用いて、駅一覧表示プログラムを作成しています。使用言語はphp,フレームワークはcodelgniterです。
まず、路線コードをブラウザのアドレス欄に指定したら、その路線コードの駅一覧が表示されるプログラムを作成しました。下記がそのコードです。
php(controllers)
<?php class Xml5 extends CI_Controller{ function index($city) { $this->load->view('Xml_open_view'); if($city != ""){ $url = 'https://~jp/ekidata/api/l/' .$city. '.xml'; $xmlstr = file_get_contents($url); $xml = simplexml_load_string($xmlstr); $w1 = $xml->line->line_code; $data = array('linecode' => $w1); } foreach($xml->station as $tmp) { $w1 = $tmp->station_name; $data = array('stationname' => $w1); $this->load->view('Xml5_out_view', $data); } $this->load->view('Xml_close_view'); } } ?>
html(open_views)
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>xml_view</title> </head> <body>
php(out_view)
<?php echo "<p>"; echo $stationname; echo "</p>"; ?>
html(close_view)
</body> </html>
これで、路線コードが11612の場合、
https://web~/~/fw/xml5/index/11612
とすると駅一覧が表示されました。
次に、同じ駅データ.jpの
都道府県API(http://www.ekidata.jp/api/api_pref.php)
を用いて、県番号をページ内で入力すると、路線コードが得られるプログラムを作成しました。
php(controllers)
<?php class Xml6 extends CI_Controller{ function index() { $this->load->view('Xml_open_view'); $this->load->view('Xml6_input_view'); $city = $this->input->post('city'); if($city != "" ){ $url='https://~.jp/ekidata/api/p/' . $city . '.xml'; $xmlstr = file_get_contents($url); $xml = simplexml_load_string($xmlstr); $w1 = $xml->pref->code; $w2 = $xml->pref->name; $data = array('prefcode' => $w1,'prefname' => $w2); $this->load->view('Xml1_out_view', $data); foreach ($xml->line as $tmp){ $w1 = $tmp->line_cd; $w2 = $tmp->line_name; $data = array("linecd" => $w1, 'linename' => $w2); $this->load->view('Xml6_out_view',$data); }} $this->load->view('Xml_close_view'); }} ?>
html(input_view)
県番号を入力してから送信をクリックしてください: <form method="post" action="https://~.jp/~/fw/xml6"> <input type="text" name="city" id="city" size="60"> <input type="submit"> </form>
php(out1_view)
<? echo "<h1>"; echo $prefcode; echo "<br/>"; echo $prefname; echo "</h1>"; ?> <br>
html(out2_view)
<a href=https://web.seinan-gu.ac.jp/~s23ag177/fw/xml5/index><?php echo $linecd. ' '.$linename;?> </a> <br>
そして、この上記のxml6のコードを修正して、県番号を入力して得られた路線コード一覧にリンク要素をつけて路線コードをクリックしたらxml5を呼んで路線コードに存在している駅一覧を表示させたいです。
xml6のout2_viewの部分にa hrefを加えてリンク要素をつけてxml5を呼ぶときの方法がわかりません。(indexの後の部分です。もしくはそれ以前にやるべきことがあるかもしれませんが。。)
xml5は元々urlに路線コードをブラウザに指定したら表示させる仕組みなのでここからどうクリックしたら駅一覧を表示させるかに難航しています。
まだ回答がついていません
会員登録して回答してみよう