How do i display data from API using the ID? - php

I can now display the correct ID for each row of data in my URL. But now i am looking to display it in another screen.
I am struggling to figure out how to pull the data such as "player" or "Team" from the ID initially retrieved.
I am looking to display this data in a table in the GameInfo page.
Any help is appreciated. Thanks
For example:
My Get for match results
==========================
public function getMatchInfo() {
$url = $this->buildUrl('scores/events.json?
key=Ka88B6jZrxO8dDQt&secret=L233yXpNCWQDZyxJIGSkNbjeI8nWLdqw&id=1"');
return $this->makeRequest($url);
}
Here i have to specify the ID which is annoying because the IDs change from game to game.
Is there a way i can edit to pull the ID from my index page? From the index page it displays the current "Live Games", and i click a link which opens the "Game Info" page, which is where i want to pull data from the API.
Index.php
======================
<?php
include 'config.php';
include 'functions.php';
include 'myapp.php';
$Api = new LivescoreApi();
$data = $Api->getLiveScores();
$timezone = 'Europe/Istanbul';
?>
<html>
<head>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-6">
<table class="table table-bordered">
<tr class="table-info">
<th>KO</th>
<th>Time</th>
<th>Home</th>
<th>Score</th>
<th>Away</th>
</tr>
<?php foreach ($data['data']['match'] as $_match) { ?>
<tr>
<td><?= convert($_match['scheduled'], $timezone) ?></td>
<td><?= $_match['time'] ?></td>
<td style="text-align: right;"><?= $_match['home_name'] ?></td>
<td style="text-align: center;"><?= $_match['score'] ?></td>
<td><?= $_match['away_name'] ?></td>
<td style="text-align: right;"><a href="gameInfo.php"> Game
information </a><?= $_match['id'] ?></td>
</tr>
<?php } ?>
</table>
</div>
<div class="col-md-3">
</div>
</div>
</div>
</body>
</html>
GameInfo.php
=================
<tr class="table-info">
<h2 style="text-align: center;"> Game Info </h2>
<th>Player Name</th>
<th>Event</th>
<th>Time</th>
</tr>
<?php foreach ($data['data']['event'] as
$_event){ ?>
<tr>
<td style="text-align: center;"><?=
$_event['player'] ?></td>
<td style="text-align: center;"><?=
$_event['event'] ?></td>
<td style="text-align: center;"><?=
$_event['time'] ?></td>

Quite possible that I don't understand your question. Create a form so that the id is sent in the $_GET["id"] variable?

I think you just want to pass the ID you already have to the function?
public function getMatchInfo() {
if(isset($_GET['id']){
$id = $_GET['id']
} //this check entirely optional, but I'd do it
else{
$id = 1; //or some other default you want, or error handling here
}
$url = $this->buildUrl('scores/events.json?
key=Ka88B6jZrxO8dDQt&secret=L233yXpNCWQDZyxJIGSkNbjeI8nWLdqw&id='.$id);
return $this->makeRequest($url);
}
Index.php
<?php
include 'config.php';
include 'functions.php';
include 'myapp.php';
$Api = new LivescoreApi();
$data = $Api->getLiveScores();
$timezone = 'Europe/Istanbul';
?>
<html>
<head>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-6">
<table class="table table-bordered">
<tr class="table-info">
<th>KO</th>
<th>Time</th>
<th>Home</th>
<th>Score</th>
<th>Away</th>
</tr>
<?php foreach ($data['data']['match'] as $_match) { ?>
<tr>
<td><?= convert($_match['scheduled'], $timezone) ?></td>
<td><?= $_match['time'] ?></td>
<td style="text-align: right;"><?= $_match['home_name'] ?></td>
<td style="text-align: center;"><?= $_match['score'] ?></td>
<td><?= $_match['away_name'] ?></td>
<td style="text-align: right;"><a href="gameInfo.php?id=<?= $_match['id'] ?>"> Game
information </a><?= $_match['id'] ?></td>
</tr>
<?php } ?>
</table>
</div>
<div class="col-md-3">
</div>
</div>
</div>
</body>
</html>

So i figured out the answer to my updated question.
I didnt add the "isSet()" check originally.
I did that now and I also was missing braces.
The joys of not using an IDE for code I guess.

Related

making a print button

I am trying to make a button that would print my database in a contact list format. Where they would be grouped under the Departments that the contacts are added to on the php forms. Like this: (BELOW IS MY CODE FOR THE CONTACT PAGE)
ADMIN
George George#snakdnasd 0282738292392
8432
IT DEPARTMENT
Tyler tyler#askdnasdksan 7492823829292 8321
I have looked around and have tried the usual commands like window.print and just print() but it just prints the actual php page.
<?php
require_once"connection.php";
$contacts = array();
$all_contacts = "select * from contacts where contact_status = '1'";
$sql_all_contacts = $conn->query($all_contacts);
$total_contacts = $sql_all_contacts->num_rows;
while ($row = mysqli_fetch_assoc($sql_all_contacts)) {
$contacts[] = $row;
}
?>
<!DOCTYPE html>
<html>
<head>
<?php include"includes/head.inc"; ?>
</head>
<body>
<div class="wrapper">
<!-- header section -->
<?php include"includes/header.inc"; ?>
<!-- content section -->
<div class="content">
<div class="floatl"><h1><?php echo $total_contacts ?> Contact(s) Total</h1></div>
<a class="floatr" href="insert_contact.php"><input class="cancel_contact_button" type="button" value="New Contact"></a>
<div class="clear"></div>
<hr class="pageTitle">
<table border ="1" style="width:100%" id="contactsTable" class="display">
<thead>
<tr align="left">
<th>Name:</th>
<th>Email:</th>
<th>Department:</th>
<th>Extension:</th>
<th>Cellphone:</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($contacts as $contact) {?>
<tr>
<td><?php echo $contact["name"];?></td>
<td><?php echo $contact["email"]; ?></td>
<td><?php echo $contact["department"]; ?></td>
<td><?php echo $contact["extension"]; ?></td>
<td><?php echo $contact["cellphone"]; ?></td>
<td><i class="fa fa-pencil"></i> | <i class="fa fa-trash-o"></i></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</body>
</html>

mysql_fetch_array(), is not work in my program

i need to fetch multiple row or single row from mysql table, but my code retrieve data from table only multiple rows as given in my where clause condition is true, not fetch single row even my condition is true. plz suggest me anyone. (i know only little bit english)
<?php
include 'connection/db_connection.php';
$sqlquery=mysql_query("select * from sadmin_invoiceno order by sno desc");
$row1=mysql_fetch_assoc($sqlquery);
$invoice=$row1['invoice_no'];
/* if($row1['invoice_no']<=9)
{
$myvalue=$row1['invoice_no'];
$idvalue="".$myvalue;
}
if($row1['invoice_no']>9 && $row1['sno']<100)
{
$myvalue=$row1['invoice_no'];
$idvalue="".$myvalue;
} */
$sql="SELECT * FROM sadmin_sales where invoice_no='".$invoice."'";
$result = mysql_query($sql);
$count=mysql_num_rows($result);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>IT Flower invoice</title>
<link rel="stylesheet" href="assets/css/style_popup.css" media="all" />
<link href="assets/css/utopia-white.css" rel="stylesheet">
</head>
<body>
<div class="invoice">
<header class="clearfix">
<div id="logo">
<img src="assets/img/utopia-logo1.png">
</div>
<div id="company">
<h2 class="name">Indian Traditional Flowers</h2>
<div>10 - 3500 McNicoll Ave., <br>Toronto, Ontario ON M1V 4c7</div>
<div>+1.647.836.9999</div>
<div>info#itflowers.com</div>
</div>
</header>
<main>
<div id="details" class="clearfix">
<div id="client">
<div class="to">INVOICE TO:</div>
<h2 class="name"><?php
$sql2=mysql_query("SELECT franchies, customer FROM sadmin_sales where invoice_no='".$invoice."'");
$res1=mysql_fetch_array($sql2);echo $res1['customer'];
$franchies= $res1['franchies'];
$sql3=mysql_query("SELECT * FROM sadmin_customer where email='".$franchies."'");
$res2=mysql_fetch_array($sql3);
echo $res2['shop_name'];
?></h2>
<div class="address"> <?php echo $res2['owner_name']; ?> </div>
<div class="address"><?php echo $res2['address']; ?></div>
<div class="email"><?php echo $res2['email']; ?></div>
<div class="email"><?php echo $res2['phone']; ?></div>
</div>
<div id="invoice">
<h1>INVOICE NO: <?php echo $invoice; ?></h1>
<div class="date">Date of Invoice: <?php echo $date = date('d/m/Y h:i:s a'); ; ?></div>
</div>
</div>
<table class="inventory" border="0" cellspacing="0" cellpadding="0">
<thead>
<tr style="text-align: center;">
<th class="no">#</th>
<th class="desc">DESCRIPTION</th>
<th class="unit">UNIT PRICE</th>
<th class="qty">QUANTITY</th>
<th class="total">TOTAL</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$tax=0;
$ship=0;
$paid=0;
$bal=0;
$grand=0;
$sum=0;
for($i=1; $i<$count;)
{
while($row = mysql_fetch_array($result))
{
$franchies = $row['franchies'];
echo "<td class='no'>".$i."</td> ";
echo "<td class='desc'>" . $row['product_name'] . "</td>";
echo "<td class='unit'>" . $row['unit_price'] . "</td>";
echo "<td class='qty'>" . $row['quantity'] . "</td>";
echo "<td class='total' >".$row['total']."</td>";
echo "</tr>";
$sum=$sum + $row['total'];
$i++;
//$tax=$sum *.13;
$include=$row['tax_type'];
if ($include =='includetax')
{
$tax1=mysql_query("select * from sadmin_tax where tax_type like 'include%'");
$res1=mysql_fetch_array($tax1);
$tax2=$res1['percentage'];
$tax=$sum * $tax2/100;
$sum1=$sum - $tax;
}
else
{
$tax3=mysql_query("select * from sadmin_tax where tax_type like 'exclude%'");
$res3=mysql_fetch_array($tax3);
$tax4=$res3['percentage'];
$tax=$sum * $tax4/100;
$sum1=$sum + $tax;
}
$ship=$row['shipping_amt'];
$paid=$row['paid_amt'];
$grand=$sum1 + $ship;
$bal=$grand - $paid;
}
}
?>
</tbody>
<tfoot>
<tr>
<td colspan="2"></td>
<td colspan="2">SUBTOTAL</td>
<td>$ <?php echo $sum; ?></td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2">TAX 13%</td>
<td>$ <?php echo $tax; ?></td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2">SHIP</td>
<td>$<?php echo $ship; ?></td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2">PAID</td>
<td>$<?php echo $paid; ?></td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2">BALANCE</td>
<td>$<?php echo $bal; ?></td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2">GRAND TOTAL</td>
<td>$<?php echo $grand; ?></td>
</tr>
</tfoot>
</table>
<button onclick="myFunction()" class="btn btn-primary" style="width: 106px;float: right;">Print this page</button>
<!-- <div id="thanks">Thank you!</div>
<div id="notices">
<div>NOTICE:</div>
<div class="notice">A finance charge of 1.5% will be made on unpaid balances after 30 days.</div>
</div>
</main>
<footer>
Invoice was created on a computer and is valid without the signature and seal.
</footer> -->
</div>
<script>
function myFunction() {
window.print();
}
</script>
</body>
</html>
I'm not certain I understand your question fully, are you saying you have multitple rows but only want one?
If so , check out MySQL "LIMIT " which will limit your SQL query to X number of rows, such as SELECT * FROM sadmin_customer where email='".$franchies." LIMIT 1
This will return only 1 row, the first row where the conditions are true.

Database info not showing from my loop

I new to php and learning and need some advice on shoing my database info within a loop which includes html, My page is blank so am wondering why my php isnt valid:
<?php include 'incudes/connect_db.php'; ?>
<div class="topbar">
<div class="topbarblock1">Dashboard</div>
<div class="topbarblock2">Username</div>
</div>
<br><br>
<div class="middle">
<div class="middleblock1">Dashboard</div>
<div class="middleblock2">
<div class="container">
<?php
$result = mysqli_query($con,"SELECT * FROM users");
while($row = mysqli_fetch_array($result)) {
?>
<h2>
User Management
<hr />
</h2>
</div>
<div class="containermain">
<table>
<tr>
<th class="th1">User Id</th>
<th class="th2">Username</th>
<th class="th3">Email Address</th>
<th class="th4">Details</th>
</tr>
<tr>
<td class="td1">#</td>
<td class="td2"><?php echo $row['username']; ?></td>
<td class="td3"><?php $row['email']; ?></td>
<th class="td4">Edit</th>
</tr>
</table>
</div>
</div>
</div>
<?php
}
?>
I'm not looking for a straight out answer I am looking for some advice on how this should be laid out and learn best practices.
Change the line that says
<td class="td3"><?php $row['email']; ?></td>
to
<td class="td3"><?php echo $row['email']; ?></td>
PHP will try to interpret the variable as some sort of command/statement otherwise and subsequently fail.
Also rethink where you wrap the loop as I previously mentioned, or else your HTML is going to end up wonky (you'll see this after you've fixed the code.)
You should also program by using error_reporting(E_ALL); at the start of your code so you can fix simple mistakes on your own

Add items to a cart from an array

I'm creating a php driven shopping cart as part of a task.
All products are currently stored in an array (I'm aware they would realistically be stored in a database, however for this task it isn't required)
I would like to know if its possible to add items to a cart from clicking a link, upon clicking proceed, it would take the user to a summary screen, displaying the cost of each item (value displayed from the array) and total cost of all items.
I am also looking to include a promotion code input box, is it possible to calculate and display a discount on button click, E.G "Update"
So far, I only have the products displayed in a table which is ok, the cart section is confusing me at the moment as well as the discount section.
<?php
$Item1 = array('SKU'=>test1, 'name'=>ProductTest1, 'Price'=>10.00);
$Item2 = array('SKU'=>test2, 'name'=>ProductTest2, 'Price'=>11.00);
$Item3 = array('SKU'=>test3, 'name'=>ProductTest3, 'Price'=>12.00);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>
<div id="container">
<div id="main">
<table>
<tr>
<th>SKU</th>
<th>Name</th>
<th>Price</th>
<th>Action</th>
</tr>
<tr>
<td><?php echo $Item1[SKU]; ?></td>
<td><?php echo $Item1[name]; ?></td>
<td><?php echo '£'. number_format($Item1[Price],2); ?></td>
<td><a href=#>Add To Cart</a></td>
</tr>
<tr>
<td><?php echo $Item2[SKU]; ?></td>
<td><?php echo $Item2[name]; ?></td>
<td><?php echo '£'. number_format($Item2[Price],2); ?></td>
<td><a href=#>Add To Cart</a></td>
</tr>
<tr>
<td><?php echo $Item3[SKU]; ?></td>
<td><?php echo $Item3[name]; ?></td>
<td><?php echo '£'. number_format($Item3[Price],2); ?></td>
<td><a href=#>Add To Cart</a></td>
</tr>
</table>
<input id="Proceed" type="Submit" value="Proceed">
</div>
</div>
</body>
</html>
Your first port of call will be to look into sessions (http://www.w3schools.com/php/php_sessions.asp)
Your Add to Cart link will go through to a different PHP page, so it might be addtocart.php
Each Add to Cart link will pass through the SKU/Product ID, so the link for each will have a unique GET parameter - I.E. addtocart.php?sku=test1 - That way you know which Product you're adding to cart.
I'd read through a couple of tutorials from Google, like http://jameshamilton.eu/content/simple-php-shopping-cart-tutorial?PHPSESSID=99d373741727e3010a32319f1ebed001
If you're still having trouble, breakdown your question into specific points and I'm sure someone will help out.
See Simple PHP Shopping Cart without SQL for creating a shopping cart without a database.
At first this is not good way to use the array like this. Try this:
<?php
$products = array(
array('SKU'=>test1, 'name'=>ProductTest1, 'Price'=>10.00),
array('SKU'=>test2, 'name'=>ProductTest2, 'Price'=>11.00),
array('SKU'=>test3, 'name'=>ProductTest3, 'Price'=>12.00),
);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>
<div id="container">
<div id="main">
<table>
<tr>
<th>SKU</th>
<th>Name</th>
<th>Price</th>
<th>Action</th>
</tr>
<?php foreach ($products as $key => $product): ?>
<tr>
<td><?php echo $product['SKU']; ?></td>
<td><?php echo $product['name']; ?></td>
<td><?php echo '£'. number_format($product['Price'],2); ?></td>
<td>Add To Cart</td>
</tr>
<?php endif; ?>
</table>
<input id="Proceed" type="Submit" value="Proceed">
</div>
</div>
</body>
</html>
Now how to solve the cart. You have to use something persistent. The session will be easiest:
<?php
session_start();
$products = array(
array('SKU'=>test1, 'name'=>ProductTest1, 'Price'=>10.00),
array('SKU'=>test2, 'name'=>ProductTest2, 'Price'=>11.00),
array('SKU'=>test3, 'name'=>ProductTest3, 'Price'=>12.00),
);
if (isset($_GET['action'] && $_GET['action'] === 'addToCart') {
if (!isset($_SESSION['cart']) $_SESSION['cart'] = array();
$_SESSION['cart'][] = $_GET['product'];
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>
<div id="container">
<div id="main">
<?php if (isset($_SESSION['cart']) && !empty($_SESSION['cart'])): ?>
<ul id="cart">
<?php foreach($_SESSION['cart'] as $product): ?>
<li><?= $products[$product]['name'] ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<table>
<tr>
<th>SKU</th>
<th>Name</th>
<th>Price</th>
<th>Action</th>
</tr>
<?php foreach ($products as $key => $product): ?>
<tr>
<td><?php echo $product['SKU']; ?></td>
<td><?php echo $product['name']; ?></td>
<td><?php echo '£'. number_format($product['Price'],2); ?></td>
<td>Add To Cart</td>
</tr>
<?php endif; ?>
</table>
<input id="Proceed" type="Submit" value="Proceed">
</div>
</div>
</body>
</html>
It should work. Just adapting your code.

Repeated page design when retrieving entry from MySQL database

I just made a PHP script to retrieve entry from database. When I retrieve entry from a table, the page look like this. The header and form below table is repeated. I need help, thanks.
Here's the whole code I'm using:
<?php
session_start();
include('connection.php');
$username='username';
mysql_query("SELECT * FROM regmember WHERE username='$username'");
$query=("SELECT * FROM product");
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="index.css">
<title>Music Light</title>
<div align="center"><h1>Music Light</h1></div>
<div id="menu">
<ul>
<li><?php echo 'Welcome, '.$_SESSION['username']; ?></li>
<li>Home</li>
<li>Product</li>
<li>Profile</li>
<li>Cart</li>
<li>Testimony</li>
<li>Transaction</li>
<li>Logout</li>
</ul>
</div>
<br>
<br>
<div align="center">
<table border="0">
<tr>
<td>ID</td>
<td></td>
<td><?php echo $row[0];?></td>
</tr>
<tr>
<td>Brand</td>
<td></td>
<td><?php echo $row[1];?></td>
</tr>
<tr>
<td>Instrument Type</td>
<td></td>
<td><?php echo $row[2];?></td>
</tr>
<tr>
<td>Price</td>
<td></td>
<td><?php echo $row[3];?></td>
</tr>
<tr>
<td>Stock</td>
<td></td>
<td><?php echo $row[4];?></td>
</tr>
<tr>
<td>Image</td>
<td></td>
<td><img height="150" width="150" src="productimg/<?php echo $row[6];?>"/></td>
</table>
</div>
<div align="center">
<table>
<form name="deleteentry" action="delete.php" method="get">
<tr>
<td>Delete which entry? (enter product id)</td>
<td><input type="text" name="delete"></td>
<td><input type="button" name="deletebutton" value="Delete"></td>
</tr>
</form>
</table>
</div>
<?php
}
?>
<br>
<br>
<div align="center"><p>Description template</p></div>
<footer>
<p align="center">Copyright © 2013 Music Light</p>
</footer>
missing </head> and <body> and </body>
i would suggest that the <html> <head></head> and <body> to be used outside the loop if u donot actually want to repeat. by loop i mean the while loop
You didn't closed your header with </head>
<head>
<link rel="stylesheet" type="text/css" href="index.css">
<title>Music Light</title>
</head> <= Include this
.
.
.
</html> <= Include this as well at the end

Categories