Display array inside arrary loop - php

I am working on a project where in I have tables of Invoices, Quotations and many more.
Where I am stuck is here -
I am saving values of quotation in to a table as a n array. Eg :-
If two services are selected then it saves it in this way : Service1, service2
Similarly its quantity and charges are saved in d same way:
Sqty : 1,1
Scharge: 100,300
Now For Invoice, I get the quotation id and service name from POST
$qdetails = mysql_query("SELECT * FROM tbl_quotation WHERE id ='$qid';")
or die(mysql_error());
$qdet = mysql_fetch_array( $qdetails );
$pcharge = $qdet['pcharge'];
$scharge = $qdet['scharge'];
$pqty = $qdet['pqty'];
$sqty = $qdet['sqty'];
Then display the output in the following table -
tbl_service_info is my table which has all the details of the service
<?php
foreach ($_POST['service'] as $key => $value) {
$query=("select * from tbl_service_info WHERE sname = '$value'");
$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );
while($row=mysql_fetch_array($result)){
$product = #$row["sname"];
$prid = #$row["id"];
$sdetails = #$row["details"];
?>
<tr class="item-row">
<td class="item-name"><div class="delete-wpr">
<textarea name="services[]"><?php echo $value ; ?></textarea></div></td>
<td class="description"><textarea><?php echo $sdetails; ?></textarea></td>
<td><textarea class="description"><?php echo $sunit; ?></textarea></td>
<?php }
$scharge1 = explode(",",$scharge);
foreach ($scharge1 as $keysc => $valuesc) { ?>
<td><textarea class="cost" name="scharge[]"> <?php
echo $valuesc;?> </textarea> </td> <?php } ?>
<?php
$sqty1 = explode(",",$sqty);
foreach ($sqty1 as $key2 => $value2) { ?>
<td><textarea class="qty" name="snos[]"><?php echo $value2;?></textarea></td>
<?php } ?>
<td><span class="price"></span></td>
</tr>
<?php } ?>
This is what i get.
http://oi61.tinypic.com/286v30j.jpg
The Unit Cost and Nos are not looping with the main loop.
Please help.
Also correct my question If I have written it the wrong way.
Thanks...

Following my comment, here is the workaround that should work in your case.
You can use a temporary value to echo the correct part of the exploded string ($charge)
$i = 0;
while($row=mysql_fetch_array($result)){
// Rest of your code
$scharge1 = explode(",",$scharge);
// Remove that foreach ($scharge1 as $keysc => $valuesc) { ?>
<td><textarea class="cost" name="scharge[]">
<?php echo $scharge1[$i];?>
</textarea></td>
// Rest of your code, also change the NOS part
// Before the very end of your while loop, increment $i
++$i;
}

Related

Two arrays in foreach loop.

I want to fill in the table. The database is filling the array, but this method didn't work. What is the problem? How to use foreach loop?
<?php
$a = $db->prepare("select *
from sabit_sayfalar
inner join alt_sayfa on sabit_sayfalar.sayfa_id = alt_sayfa.ustsayfaid");
$a->execute(array());
$b = $a->fetchALL(PDO::FETCH_ASSOC);
$c = $a->rowCount();
$q = $db->prepare("select *
from sabit_sayfalar
inner join alt_sayfa on sabit_sayfalar.sayfa_id = alt_sayfa.altsayfaid");
$q->execute(array());
$w = $q->fetchALL(PDO::FETCH_ASSOC);
$e = $q->rowCount();
if($c){
foreach($b as $w and $m as $n){
?>
<tbody>
<tr>
<td>
<?php echo $m["sayfa_adi"];?>
</td>
<td>
<?php echo ($n["sayfa_adi"]);?>
</td>
</tr>
<?php
}
}
?>
</tbody>
Here's what I came out with.
<?php
$firstQuery = $db->prepare("select *
from sabit_sayfalar
inner join alt_sayfa on sabit_sayfalar.sayfa_id = alt_sayfa.ustsayfaid");
$firstQuery->execute(array());
$firstQueryResults = $firstQuery->fetchALL(PDO::FETCH_ASSOC);
$firstQueryCount = $firstQuery->rowCount();
$secondQuery = $db->prepare("select *
from sabit_sayfalar
inner join alt_sayfa on sabit_sayfalar.sayfa_id = alt_sayfa.altsayfaid");
$secondQuery->execute(array());
$secondQueryResults = $secondQuery->fetchALL(PDO::FETCH_ASSOC);
$secondQueryCount = $secondQuery->rowCount();
if ($firstQueryCount > 0 && $secondQueryCount > 0) {
?>
<tbody>
<?php foreach ($firstQueryResults as $firstQueryKey => $firstQueryRow) { ?>
<tr>
<td>
<?php echo $firstQueryRow["sayfa_adi"];?>
</td>
<td>
<?php echo ($secondQueryResults[$firstQueryKey]["sayfa_adi"]);?>
</td>
</tr>
<?php
}
}
?>
</tbody>
I changed the names of the variables for readability, and I adapted the table for using the information you provided... there are still some flaws connected with indexes that might be different for each SELECT, but that is another problem.
I also interpreted some variables that were missing as the variables you had but, for some reason, wrote wrong.
you cannot be used and in a foreach i think you want to use 2 foreach here and your <tbody> should be out of the loop

Display columns with rows in php table

Hey guys I need your help, I have this table
and I want to shows like this FIDDLE, really I don't know how to do this because sometimes just exist two columns(prov_name ) and sometimes exist more that two rows please help me if you can !
Hope you understand me. Thanks so much !
In this way I can be able to select data from Joomla.
$db =& JFactory::getDBO();
$query = 'SELECT prov_name FROM provprices where CA_id = '.$CA_id;
$db->setQuery($query);
$result = $db->loadObjectList();
$prov_name = $result[0];
echo $prov_name->prov_name;
First off, in order for your data to be presented like that obviously it must be grouped accordingly.
The first row is, the prov_name's, so you can use GROUP BY or you cal also do it in PHP. Based of the sample data, it should have from 1 to 6.
Then the second row is just a simple unitval and totval according to how many prov_name's.
Third is the and the rest is the grouping of the values. See Example:
$db = new PDO('mysql:host=localhost;dbname=DATABASE_NAME;charset=utf8', 'USERNAME', 'PASSWORD');
$data = array();
$results = $db->query("SELECT * from YOUR_TABLE_NAME");
while($row = $results->fetch(PDO::FETCH_ASSOC)) {
$data[$row['prov_name']][] = $row;
}
$keys = array_keys($data);
$size = count($keys);
$vals = array();
// grouping:
// if there are six (cam1 to cam6)
// then group them by cam1, ... to cam6, then repeat until theres no more left
while(count($data) > 0) {
foreach($keys as $key) {
if(!empty($data[$key])) {
$vals[] = array_shift($data[$key]);
} else {
unset($data[$key]); // remove them if empty
}
}
}
$vals = array_chunk($vals, $size); // split them by how many prov_names
?>
<table border="1" cellpadding="10">
<!-- PROV NAMES -->
<tr><?php for($x = 1; $x <= $size; $x++): ?>
<th colspan="2"><?php echo "prov_name $x"; ?></th>
<?php endfor; ?></tr>
<!-- unitval totvals -->
<tr><?php for($x = 1; $x <= $size; $x++): ?>
<td>unitval</td><td>totval</td>
<?php endfor; ?></tr>
<!-- the grouped values -->
<?php foreach($vals as $val): ?>
<tr>
<?php foreach($val as $v): ?>
<td><?php echo $v['unitval']; ?></td>
<td><?php echo $v['totval']; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
I think you must do this first,
try looping to show prov_name in horizontal,
and then you fetch again in query with
"SELECT * FROM table_test where prov_name = '$prov_name'"
in Variable $prov_name you must have a value with CAM2 or CAM3 .etc
sorry just a logic :)

Output a count number in foreach loop to show proper row number

What's the best practice to output the each row's real counter from an array ? I have the object array of retrieved users from database that I'am echoing into the <table />
<tbody>
<?php $i = 1;?>
<?php foreach($members as $member):?>
<tr>
<td class="column-counter">
<?php echo $i;?>
</td>
<td class="column-check">
<input type="checkbox" class="checkbox" name="checked[]" value="<?php echo $member->id;?>" <?php if(intval($member->id) === intval($this->session->userdata('login_status')['id'])):?>disabled<?php endif;?>>
</td>
<td class="column-username">
<?php echo $member->username;?>
</td>
<td class="column-email">
<?php echo $member->email;?>
</td>
<td class="column-id">
<?php echo $member->id;?>
</td>
</tr>
<?php $i++;?>
<?php endforeach;?>
</tbody>
What I've done so far is that $i =1 incrementing each time the loop is triggered.
But the problem is that if I go to the second page, it starts from "1" again, instead of let's say 21 (in case it shows 20 rows per page).
How can I make it right so it will continue counting from last row of previous page ?
By the way I'am using a codeigniter if that helps.
==== UPDATE ====
The model models/members_model.php i'am using in controllers/members.php to retrieve the users holds this function mixed with pagination:
public function members($data = array(), $return_count = FALSE){
$this->db
->select('
members.id,
categories.title as role,
members.catid as role_id,
members.firstname,
members.lastname,
members.username,
members.email,
members.status,
members.image
')
->join('categories', 'members.catid = categories.id');
if( empty($data) ){
// If nothing provided, return everything
$this->get_members();
}else{
// Grab the offset
if( !empty($data['page']) ){
$this->db->offset($data['page']);
}
// Grab the limit
if( !empty($data['items']) ){
$this->db->limit($data['items']);
}
if( $return_count ){
return $this->db->count_all_results($this->_table_name);
}else{
return $this->db->get($this->_table_name)->result();
}
}
}
It sounds like $data['page'] holds the information you need since this is related to the specified offset. You didn't reference the name of this array in your global scope, so I will just call this $data_array in my answer.
You can determine the starting value for $i as follows
$i = $data_array['page'] + 1;

How do output combined results from two MySQL tables using PDO

I have the following two table structures in MySQL, which record details of a conference call and those participants that joined it:
Table: conference:
conference_sid, date_created, date_completed, RecordURL, PIN
*date_created and *date_completed are timestamps
Table: participants:
conference_sid, call_sid, call_from, name_recording
I want to output a simple table, that displays the following results for each conference_sid as a separate row:
<table>
<thead>
<th>Date</th>
<th>Duration</th>
<th>Participants</th>
<th>Recording</th>
</thead>
<tbody>
<tr id="conference_sid">
<td>date_created</td>
<td>duration: [date_completed - date_created in h/mm/ss]</td>
<td>
<li>call_from [for all participants in that conference_sid]
<li>call_from...
</td>
<td>
Call recording
</td>
</tr>
<tr id="conference_sid">
...
</tr>
</tbody>
</table>
I only want this table to show relevant results for conferences that have the same PIN as the user's Session::get('PIN')
You can combine the participants using GROUP_CONCAT
SELECT
conf.conference_sid,
date_created,
TIMEDIFF(date_completed, date_created) AS duration,
conf.RecordURL,
conf.PIN,
GROUP_CONCAT(pid SEPARATOR ",") AS pid,
GROUP_CONCAT(call_sid SEPARATOR ",") AS call_sid,
GROUP_CONCAT(call_from SEPARATOR ",") AS call_from,
GROUP_CONCAT(name_recording SEPARATOR ",") AS name_recording
FROM
conference conf
LEFT OUTER JOIN
participants p ON p.conference_sid = conf.conference_sid
WHERE
conf.PIN = 123
GROUP BY conf.conference_sid
Refer SQLFIDDLE and MySQL documentation about TIMEDIFF.
Now the application logic will be
<?php
$pin = 123;
$db = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
$stmt = $db->prepare(
'SELECT
conf.conference_sid,
date_created,
timediff(date_completed, date_created) AS duration,
conf.RecordURL,
conf.PIN,
GROUP_CONCAT(pid SEPARATOR ",") AS pid,
GROUP_CONCAT(call_sid SEPARATOR ",") AS call_sid,
GROUP_CONCAT(call_from SEPARATOR ",") AS call_from,
GROUP_CONCAT(name_recording SEPARATOR ",") AS name_recording
FROM
conference conf
LEFT OUTER JOIN
participants p ON p.conference_sid = conf.conference_sid
WHERE
conf.PIN = :pin
GROUP BY conf.conference_sid');
$stmt->bindParam(':pin', $pin);
?>
<table border="1">
<thead>
<th>Date</th>
<th>Duration</th>
<th>Participants</th>
<th>Recording</th>
</thead>
<tbody>
<?php
$stmt->execute();
while ($row = $stmt->fetch()) {
?>
<tr>
<td><?php echo $row['date_created']; ?></td>
<td><?php echo $row['duration']; ?></td>
<td>
<table border="1">
<thead>
<th>call_sid</th>
<th>call_from</th>
<th>name_recording</th>
</thead>
<tbody>
<?php
$length = count(explode(',', $row['pid']));
$call_sid = explode(',', $row['call_sid']);
$call_from = explode(',', $row['call_from']);
$name_recording = explode(',', $row['name_recording']);
for ($i=0; $i < $length; $i++) {
?>
<tr>
<td> <?php echo $call_sid[$i]; ?> </td>
<td> <?php echo $call_from[$i]; ?></td>
<td> <?php echo $name_recording[$i]; ?> </td>
<tr>
<?php
}
?>
</tbody>
</table>
</td>
<td>
<a href="<?php echo $row['RecordURL']; ?>">
Call recording</a>
</td>
</tr>
<?php
}
?>
</tbody>
You will get the result set with comma(,) separated values in pid, call_sid, call_from, and name_recording. You can convert this string to array using explode.
array explode ( string $delimiter , string $string [, int $limit ] )
Returns an array of strings, each of which is a substring of string
formed by splitting it on boundaries formed by the string delimiter.
I won't do the PHP part, as I am not that knowledgeable in PHP, but here is the SQL:
SELECT *
FROM `conference`, `participants`
WHERE `conference`.PIN = $PIN AND
`participants`.conference_sid = `conference`.conference_sid
This will return rows with the information from conference and the participants of those conferences, joined into one row.
The following query will give you the information you need to display:
SELECT c.conference_sid
, c.date_created
, timediff(c.date_completed, c.date_created) AS duration
, p.call_from
, p.name_recording
, c.RecordURL
FROM conference c
JOIN participants p
ON c.conference_sid = p.conference_sid
WHERE c.PIN = :PIN
ORDER BY c.conference_sid
You will need to process the results with a nested loop. The outer loop should advance each time the conference_sid changes. The inner loop will display each element of the participants list for that conference.
This will be my take on it, it uses 2 separate queries to keep the data kinda separated. I use fetchAll() for brevity but this could have performance issues, luckily this can be accomodated. I didn't put any error checking, if you want it or you have questions, please ask
<?php
// assume $db is a PDO connection to the database
/* #var $db PDO */
$q = 'SELECT conference_sid, date_created, date_completed, RecordURL, PIN'
.' FROM conference';
// we need these
$conferences = $db->query($q)->fetchAll(PDO::FETCH_CLASS,'stdClass');
// let's group them as CSV, and concatenate the contents with ":"
$q = 'SELECT conference_sid,GROUP_CONCAT(CONCAT_WS(":",call_from,name_recording)) AS parts '
.' FROM participants GROUP BY conference_sid';
$conf_parts = array();
foreach ($db->query($q)->fetchAll(PDO::FETCH_CLASS,'stdClass') as $parts) {
// save the participants as an array, their data is still joined though
$conf_parts[$parts->conference_sid] = explode(',',$parts->parts);
// their contents will be exploded later
}
?>
<table>
<thead><th>Date</th><th>Duration</th><th>Participants</th><th>Recording</th></thead>
<tbody><?php foreach ($conferences as $conference) {
$csid = $conference->conference_sid;
// http://stackoverflow.com/questions/3108591/calculate-number-of-hours-between-2-dates-in-php
// Create two new DateTime-objects...
$date1 = new DateTime($conference->date_completed);
$date2 = new DateTime($conference->date_created);
// The diff-methods returns a new DateInterval-object...
$diff = $date2->diff($date1);
?><tr id="<?php echo $csid; ?>">
<td><?php echo $conference->date_created; ?></td>
<td><?php echo $diff->format('H/i/s'); ?></td>
<td>
<ul><?php foreach ($conf_parts[$csid] as $participant) {
// we have each participant for this conference call
list ($call_from, $name_recording) = explode($participant,':');
// and now we have the required data from each participant
?><li><?php echo $call_from; ?></li><?php
} ?></ul>
</td>
<td>
Call recording
</td>
</tr><?php
} ?></tbody>
</table>
In this particular contex I prefer to use two separated queries. Here's how I would do it:
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Could not connect to db';
exit;
}
$stmt_conferences = $db->prepare(
'SELECT
date_created,
timediff(date_completed, date_created) AS duration,
RecordURL,
conference_sid
FROM
conference
WHERE
PIN=:pin');
$stmt_conferences->bindParam(':pin', $pin);
$stmt_participants = $db->prepare(
'SELECT
name_recording,
call_from
FROM
participants
WHERE
conference_sid=:confsid');
$stmt_participants->bindParam(':confsid', $confsid);
?>
<table>
<thead>
<th>Date</th>
<th>Duration</th>
<th>Participants</th>
<th>Recording</th>
</thead>
<tbody>
<?php
$pin = 1; /* get your PIN here */
$stmt_conferences->execute();
while ($row = $stmt_conferences->fetch()) {
?>
<tr>
<td><?php echo htmlspecialchars($row['date_created'], ENT_QUOTES); ?></td>
<td><?php echo htmlspecialchars($row['duration'], ENT_QUOTES); ?></td>
<td>
<?php
$confsid = $row['conference_sid'];
$stmt_participants->execute();
while ($participant = $stmt_participants->fetch()) {
?>
<li><a href="<?php echo htmlspecialchars($participant['name_recording'], ENT_QUOTES); ?>">
<?php echo htmlspecialchars($participant['call_from'], ENT_QUOTES); ?>
</a>
<?php
}
?>
</td>
<td>
<a href="<?php echo htmlspecialchars($row['RecordURL'], ENT_QUOTES); ?>">
Call recording</a>
</td>
</tr>
<?php
}
?>
</tbody>
Please notice that you have to add some code to handle errors and to correctly escape all data you echo (can you really trust your database?). Also element IDs should be unique within the entire document, you can have just one id="conference_sid" in your page. Use classes instead.
Edit
If you can really trust your database, then you can just output the contents of a field with code like this:
<?php echo $row['call_from']; ?>
but what happens if RecordURL contains for example the following string?
<script>alert("I am injecting some code....");</script>
It will happen that some unwanted code will be injected in your page, so it is always better yo use a safe function like htmlspecialchars() every time you need to echo some output:
<?php echo htmlspecialchars($row['call_from'], ENT_QUOTES); ?>
this way, any unwanted code won't be harmful.
I also added a basic TRY/CATCH construct to handle errors.
Hope this helps!
First, we need to get our result.
$vPIN = $_SESSION['PIN']; // or however you get your user's pin from session
$vQuery = "SELECT * FROM conference AS a LEFT JOIN participants as B USING (conference_sid) WHERE a.PIN='$vPIN'";
$oResult = $oDB->execute($vQuery);
$aRows = $oResult->fetchAll(PDO::FETCH_ASSOC);
note the prefixes: $v if for a simple variable, $o represents a ressource (which I like to think of as an object), $a represents an array. It's just for my mental sanity.
so now, we have an array, probably very big, containing every single row in the conference table times every corresponding row in the participants. Sweet, now let's build an array with some meaning in it.
foreach($aRows as $aRow) // maybe a bit confusing but the 's' changes everything: all rows vs one row
{if (!isset($aConferences[$aRow['conference_sid']]['infos']))
{$aConferences[$aRow['conference_sid']]['infos']['date_created'] = $aRow['date_created'];
$aConferences[$aRow['conference_sid']]['infos']['date_completed'] = $aRow['date_completed'];
$aConferences[$aRow['conference_sid']]['infos']['record_url'] = $aRow['RecordURL'];
$aConferences[$aRow['conference_sid']]['infos']['pin'] = $aRow['PIN'];}
$aConferences[$aRow['conference_sid']]['participants'][] = $aRow['call_from'];}
so what happens here is that for each row, if the infos for corresponding conference_sid haven't been set, they will be, and then we create a list from 0 to x of each call_from for that conference. print_r of that array with dummy values:
[1627]['infos']['date_created'] = 2013-11-26
['date_completed'] = 2013-11-29
['record_url'] = 'http://whatever.com'
['PIN'] = 139856742
['participants'][0] = Bob
[1] = gertrude
[2] = Foo
[8542]['infos']['date_created'] = 2013-12-01
['date_completed'] = 2013-12-02
['record_url'] = 'http://whateverelse.com'
['PIN'] = 584217
['participants'][0] = Family Guy
[1] = aragorn
[2] = obama
[3] = Loki
so here is a nice array with which we can build a html table! let's do that
$vHTML = '<table>
<thead>
<th>Date</th>
<th>Duration</th>
<th>Participants</th>
<th>Recording</th>
</thead>
<tbody>';
foreach ($aConferences as $conference_sid => $aConference) // notice the s and no s again
{$vHTML.= '<tr id="' . $conference_sid . '">';
$vDateCreated = $aConference['infos']['date_created'];
$vDateCompleted = $aConference['infos']['date_completed'];
$vHTML.= '<td>' . $vDateCreated . '</td>';
$vHTML.= '<td>' . date('Y-m-d',(strtotime($vDateCompleted) - strtotime($vDateCreated))) . '</td>'; // you might have to debug that date diff for yourself.
$vHTML.= '<td><ul>'; // here a foreach for the participants
foreach ($aConference['participants'] as $call_from)
{$vHTML.= '<li>' . $call_from . '</li>';}
$vHTML.= '</ul></td>';
$vHTML.= '<td>' . $aConference['infos']['record_url'] . '</td>';
$vHTML.= '</tr>';}
$vHTML.= '</tbody></table>';
so here: for each conference create a table row with the infos, then for each participant, add a list item within the list. comment if you wish for any precision.
oh, and don't forget to do something with $vHTML. like echo $vHTML :)

how to associate column record with table header?

I have a mysql table, one of its columns called parking space has a number in it. However, for some records the 'parking space'-column is empty. Now, I want to call just this column in a page table where the column heads are numbered from 1 - 200 (first column: 1; second column: 2;....) So, if there is the value '12' in the 'parking space'-column, then this shall show up in column '12' and so on, and if there is no entry for a number then the column in the page table shall be left empty. How can I associate the numbers in 'parking space' with that page table?
...
<?php
$pagetable=array('1','2','3','4','5','6');//...until 200
foreach($pagetable as $value){
?>
<table border="1px" cellspacing="1" cellpadding="1">
<tr>
<th>
<?php echo $value ?>
</th>
</tr>
<?php
}
include("dbinfo.inc.php");
include_once("config.php");
$result = mysql_query("SELECT * FROM contacts ORDER BY park ASC");
$results = mysql_num_rows($result);
if ($results > 0){
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$park=mysql_result($result,$i,"park");
?>
<tr>
<td style="background-color:;">
<?php echo $park; ?>
<br>
<?php if($park!=''){ ?>
<?php echo $id; ?>
<?php
}
?>
</td>
</tr>
<?php
$i++;
}
}
?>
...
There are two ways, you can do this.
Create all 200 rows on the database table and query it. This will omit almost all of the headache to get what you want.
Do a for loop from 1 to 200 and compare the active item if it exists on the mysql results. For this you require an continuous numeric entry in the database to identify each columns individually.
What I mean is? Column 1 in the should have corresponding record indicating 1 somewhere in its record.
Here is an example of this:
$result = mysql_query("SELECT * FROM contacts ORDER BY park ASC");
$results = mysql_num_rows($result);
if ($results > 0) {
$num=mysql_numrows($result);
$rows = array();
while($row = mysql_fetch_row($result)) {
$rows[$row['id']] = $row;
});
//Now process it
}
Try this:
<?php
include("dbinfo.inc.php");
include_once("config.php");
$spaces = range(1, 200);
$results = mysql_query("SELECT * FROM contacts ORDER BY park ASC");
$results_count = mysql_num_rows($result);
if ($results_count > 0) {
for ($i = 0; $i < $results_count; $i++) {
$parks[] = mysql_result($results, $i, "park");
}
}
?>
<table>
<tr>
<?php foreach ($spaces as $space): ?>
<td><?php echo $space ?></td>
<?php end foreach ?>
</tr>
<tr>
<?php foreach ($parks as $park): ?>
<td><?php if ($park > 0) { echo "$space"; } else { echo " "; } ?></td>
<?php end foreach ?>
</tr>
</table>
Start with this and tell me what you get.
Ok, I got it changed a little. Now it ahows me all from 1-200 and it shows the parking spaces values, but these values are not associated with the numbers. it looks to me like it puts them according to the id, for example in my table the customer on parking space 165 has the id 4 and therefore it doesnt put 165 to 165 but 165 to 4
<?php
include("dbinfo.inc.php");
include_once("config.php");
$spaces = range(1, 200);
$results = mysql_query("SELECT * FROM contacts");
$results_count = mysql_num_rows($results);
if ($results_count > 0) {
for ($i = 0; $i < $results_count; $i++) {
$parks[] = mysql_result($results, $i, "park");
}
}
?>
<table>
<tr>
<?php foreach ($spaces as $space){ ?>
<td><?php echo $space; ?></td>
<?php } ?>
</tr>
<tr>
<?php foreach ($parks as $park){ ?>
<td><?php if ($park!='') { echo $park.'<br>';} else { echo " "; } ?></td>
<?php }?>
</tr>
</table>

Categories