php - retrieving and echoing multiple values from mysql column -
i have created these 3 columns in mysql table can have list of each users transactions.
ticket_date, ticket_num, ticket_result
i created function echo value in columns , worked successfully.
function.php:
$sql = "select * members user_id = '{$_session['user_id']}'"; $query = $this->db_connection->query($sql); while ($row = $query->fetch_object()) { global $me, $me2, $date; $me = $row->ticket_num; $me2 = $row->ticket_result; $date = $row->ticket_date; }
transactions.php
<table class="table"> <thead> <th>date</th> <th>ticket id</th> <th>result</th> </thead> <tr> <td><?php echo $date; ?> </td> <td><?php echo $me; ?> </td> <td><?php echo $me2; ?> </td> </tr> </table>
my problem if user has more 1 transaction how able echo each value particular column separately. trying echo each transaction . able store each value array call this?
$row->ticket_num['0']
edit: meant ask how can store transactions respective columns. such storing more 1 $ticket_date apply each transaction. store information columns array can call each transaction using array pointer?
i think best thing do, not use global variables this, unless there reason function can't return data, i.e. it's returning else. have in function.php:
$sql = "select * members user_id = '{$_session['user_id']}'"; $query = $this->db_connection->query($sql); $return = array(); while ($row = $query->fetch_object()) { array_push($return, array($row->ticket_num, $row->ticket_result, $row->ticket_date )); } return $return
then can in transactions.php:
<table class="table"> <thead> <th>date</th> <th>ticket id</th> <th>result</th> </thead> <?php $ticket = whatever_function(); foreach($ticket $t){ echo"<tr> <td> ".$t[0]." </td><td>".$t[1]."</td><td>".$t[2]."</td></tr>"; } ?> </table>
edit
in relation additional question in comments, database structure should set up:
by doing this, separating it, every table belongs 1 thing or action. these can related each other using sql join this:
select * transactions tr join users u on u.uid = tr.uid join tickets ti on ti.tid = tr.tid
by looking @ above sql code snippet, should able see how matches columns on different tables. creates 1 big virtual table can search stuff with, column names prepended given pseudonyms.
so if wanted email address of bought ticket price on £20, though tables need aren't directly connected do:
select u.email transactions tr join users u on u.uid = tr.uid join tickets ti on ti.tid = tr.tid ti.price > 20
Comments
Post a Comment