Saturday 31 October 2015

Web Service in php

<?php 
  /* soak in the passed variable or set our own */
  $format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
  /* connect to the db */
  $link = mysql_connect('localhost','envisiy9_update','T?pAT*{nC58E') or die('Cannot connect to the DB');
  mysql_select_db('envisiy9_SiteCMS',$link) or die('Cannot select the DB');
  /* grab the posts from the db */
  $query = "SELECT * FROM artists";
  $result = mysql_query($query,$link) or die('Errant query:  '.$query);
  /* create one master array of the records */
  $posts = array();
  if(mysql_num_rows($result)) {
    while($post = mysql_fetch_assoc($result)) {
      $posts[] = array('a_id'=>$post['a_id'],'artist_name'=>htmlentities($post['artist_name']));
    }
  }
  /* output in necessary format */
  if($format == 'json') {
   header('Content-type: application/json');
    echo json_encode(array('posts'=>$posts));
  }
  else {
    //header('Content-type: text/xml');
    echo '';
    foreach($posts as $index => $post) {
      if(is_array($post)) {
        foreach($post as $key => $value) {
          echo '<',$key,'>';
          if(is_array($value)) {
            foreach($value as $tag => $val) {
              echo '<',$tag,'>',htmlentities($val),'</',$tag,'>';
            }
          }
          echo '</',$key,'>';
        }
      }
    }
    echo '';
  }
  /* disconnect from the db */

?>


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

web service 2:

<?php 
  $link = mysql_connect('localhost','root','') or die('Cannot connect to the DB');
  mysql_select_db('angelfi4_cart',$link) or die('Cannot select the DB');
  /* grab the posts from the db */
  $query = "SELECT * FROM artists";
  $result = mysql_query($query,$link) or die('Errant query:  '.$query);
  /* create one master array of the records */
  $posts = array();
   $arts =array();
  if(mysql_num_rows($result)) {
    while($post = mysql_fetch_assoc($result)) {
   
     $query2 = "SELECT * FROM art where artist='".$post['a_id']."'";
     $result2 = mysql_query($query2,$link) or die('Errant query:  '.$query2);
    while($art = mysql_fetch_assoc($result2)) {
      $arts[] = array('image'=>'http://69.195.124.130/~envisiy9/manager/pictures/'.$art['image'],'price'=>htmlentities($art['price']),'title'=>htmlentities($art['title']),'dimensions'=>htmlentities($art['dimensions']));
    }

     $posts[] = array('a_id'=>$post['a_id'],'artist_name'=>htmlentities($post['artist_name']),'arts'=>$arts);
    
    }
  }
 
echo "<pre>";
print_r($posts);
 //header('Content-type: application/json');
 //echo json_encode(array('posts'=>$posts));



?>


No comments:

Post a Comment