Superglobal GET using PHP - php

I am new to php and I couldn't find the answer on stack. I am experimenting and trying to figure out how I can use the superglobal GET to display an image and also change the *count depending on how the user modifies it in the URL.
I would like to change the Beach image count times.
So the URL would look something like:
website.com/table?beach_image=2&count=4
and would display the beach image #2, 4 times:
bathing suit
<?php
$beach = ['sunglasses.jpg','suncreen.jpg','bathing-suit.jpg','hat.jpg'];
$count = 1;
$beach_image = filter_input(INPUT_GET,"beach_image");
?>
for my html:
<table>
<tr>
<th>Beach Image</th>
<th>Beach</th>
</tr>
<tr>
<td>0</td>
<td>Sunglasses</td>
</tr>
<tr>
<td>1</td>
<td>Sunscreen</td>
</tr>
<tr>
<td>2</td>
<td>Bathing Suit</td>
</tr>
<tr>
<td>3</td>
<td>Hat</td>
</tr>
</table>
<div>
<img src="images/<?= $beach[$beach_image]?>">
</div>

You can do it like below
<?php
$beach = ['sunglasses.jpg','suncreen.jpg','bathing-suit.jpg','hat.jpg'];
$keysArr = array_fill(0,count($beach),0);//array_combine(array_keys($beach),array_fill(0,count($beach),0));
$beach_image = filter_input(INPUT_GET,"beach_image");
$count = filter_input(INPUT_GET,"count");
$keysArr[$beach_image] = $count;
?>
<table>
<tr>
<th>Beach Image</th>
<th>Beach</th>
</tr>
<tr>
<td><?php echo $keysArr[0] ;?></td>
<td>Sunglasses</td>
</tr>
<tr>
<td><?php echo $keysArr[1] ;?></td>
<td>Sunscreen</td>
</tr>
<tr>
<td><?php echo $keysArr[2] ;?></td>
<td>Bathing Suit</td>
</tr>
<tr>
<td><?php echo $keysArr[3] ;?></td>
<td>Hat</td>
</tr>
</table>
<div>
<?php for($i=1;$i=$count;$i++){?>
<img src="images/<?= $beach[$beach_image]?>">
<?php }?>
</div>

Related

Table inside nested Table

I am having a nested table with a while loop, I want to add one more nested table in the same row:
Now I want to add one more nested table as each cd contains more than one data like below:
My code is as follows
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td><?php echo $rowcd['well_no'] ?></td>
<td><?php echo $rowcd['well_name'] ?></td>
<td>
<table border="1" width="100%">
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
/* I want to add one more nested table here*/
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
I tried some thing like this,after my second while loop
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
<td>
<table>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<tr>
<td><?php echo $rowl['logs'] ?></td>
</tr>
<?php
}
?>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
but the result was messed up. I am confused, where I want to end my while loop, I think.
Finally i got as i wish, and i am sharing the code as below
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div class="container">
<table border="1" align="center" border-collapse="collapse">
<thead>
<tr >
<th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th width="100">CD No:</th>
<th width="150">Logs</th>
<th width="100">Bottom Depth</th>
<th width="100">Top Depth</th>
<th width="100">Date of Log</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td align="center"><?php echo $rowcd['well_no'] ?></td>
<td align="center"><?php echo $rowcd['well_name'] ?></td>
<td colspan="5">
<table rules="all">
<tr>
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<td width="100" align="center"><?php echo $rowcd['cd_no'] ?></td>
<td colspan="4">
<table rules="all">
<tr>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<td width="155"><?php echo $rowl['logs'] ?></td>
<td width="105" align="center"><?php echo $rowl['bottom'] ?></td>
<td width="100" align="center"><?php echo $rowl['top'] ?></td>
<td width="100" align="right"><?php echo $rowl['date'] ?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
?>
</table>
</td>
<?php
}
}
?>
</tr>
</table>
I hope this is what you meant as per your data table shown above
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<tr>
<td>id</td>
<td>well</td>
<td>name</td>
<td>
<table border="1" width="100%">
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
</table>
</td>
<td>
<table border="1" width="100%">
<tr>
<td>Log1</td>
</tr>
<tr>
<td>Log2</td>
</tr>
</table>
</td>
</tr>
</table>
</div>

While loop to get PHP data into two different tables in HTML

Using a while loop to get my data out from PHP will only show all my data is vertical in a table form.
How can I show the data in horizontal form:
<?php
while ($display = mysqli_fetch_array($link)) {
if ($display['see_id'] % 2 == 0) {
?>
<table border='0' width='550px'>
<tr height='50px' style="text-align:center">
<td><?php echo $display['name']; ?></td>
</tr>
<tr>
<td><?php echo $display['video']; ?></td>
</tr>
<tr height='50px'>
<td><?php echo $display['description']; ?></td>
</tr>
<tr height='50px'>
<td></td>
</tr>
</table>
<?php }}
?>
You can put some style in your table: style="width:50%; float:left"
With while loop, of course.
<table border='0' style="width:50%; float:left">
<tr height='50px' style="text-align:center">
<td>Name</td>
</tr>
<tr>
<td>Video</td>
</tr>
<tr height='50px'>
<td>Description</td>
</tr>
<tr height='50px'>
<td></td>
</tr>
</table>
<table border='0' style="width:50%; float:left">
<tr height='50px' style="text-align:center">
<td>Name</td>
</tr>
<tr>
<td>Video</td>
</tr>
<tr height='50px'>
<td>Description</td>
</tr>
<tr height='50px'>
<td></td>
</tr>
</table>

How to add a class to last table row

I want to add a class to every last <tr> of a main category. I don't want to add the classname to every <tr>. What can I change in my script?
Like this:
<table>
<tr>
<th></th>
</tr>
<tr>
<td></td>
</tr>
<tr class="test">
<td></td>
</tr>
<th></th>
<tr>
<td></td>
</tr>
<tr class="test">
<td></td>
</tr>
</table>
<table class="data forum">
<? foreach ($this->mainCategories as $mainCategory): ?>
<tr>
<th><strong><?= $this->escape($mainCategory->fcName) ?></strong></th>
<th> </th>
<th><strong>Topics</strong></th>
<th><strong>Laatste topic</strong></th>
</tr>
<? foreach ($mainCategory->getSubCategories() as $category): ?>
<tr>
<td><?= $this->escape($category->getName()) ?></td>
<td><?= $this->escape($category->fcDescription) ?></td>
<? if ($this->escape($category->numTopics) > 0): ?>
<td><?= $this->escape($category->numTopics) ?></td>
<td><?= date_create($this->escape($category->last_topic))->format('d-m-Y H:i') ?></td>
<? else: ?>
<td> </td>
<td> </td>
<? endif ?>
</tr>
<? endforeach ?>
<tr>
<td class="split"></td>
</tr>
<? endforeach ?>
</table>
Grab the count of your $this->mainCategories variable and test for the last one against a counter variable...
$count = count($this->mainCategories;
$current = 0;
<?
foreach ($this->mainCategories as $mainCategory):
$current++;
?>
<tr<?= $current == $count?' class="someClass"':''?>>
Update:
Let's look at a simplified version as a proof of concept:
<?php
$mainCategories = array("Eins", "Zwei", "Drei");
$count = count($mainCategories);
$current = 0;
foreach ($mainCategories as $mainCategory):
$current++;
?>
<tr<?= $current == $count?' class="someClass"':''?>>
<td><?= $mainCategory ?></td>
</tr>
<?php endforeach ?>
This produces the following HTML:
<tr>
<td>Eins</td>
</tr>
<tr>
<td>Zwei</td>
</tr>
<tr class="someClass">
<td>Drei</td>
</tr>

putting an array into a html table

$result is actually an array which looks like this:
Array ( [book_title] => Bioethics in the 21st Century [id] => 1424
[isbn] => 978-953-307-270-8 [unix_name] =>
bioethics-in-the-21st-century [visible_online] => 1 )
This is my view(better said...poor attempt of a view ). I'm trying to get an alignment based on the index of the array. Like so:
http://pastebin.com/z13PZWe8
<table class="datagrid grid_collapsible" width="100%" cellpadding="2" cellspacing="0" id="webbooks_table">
<thead>
<tr class="datagrid_header"
<td>Book title</td>
<td>ID</td>
<td>ISBN</td>
<td>Is it visible online?</td>
</tr>
</thead>
<tbody>
<?php foreach($this->basicBwDetails as $result): ?>
<tr>
<td><?=$result;?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Thank you for your help!
Are you trying to do this?
<table class="datagrid grid_collapsible" width="100%" cellpadding="2" cellspacing="0" id="webbooks_table">
<thead>
<tr class="datagrid_header">
<td>Book title</td>
<td>ID</td>
<td>ISBN</td>
<td>Is it visible online?</td>
</tr>
</thead>
<tbody>
<?php foreach($this->basicBwDetails as $result): ?>
<tr>
<td><?php echo $result['book_title']; ?></td>
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['isbn']; ?></td>
<td><?php echo ($result['visible_online']) ? 'Yes' : 'No'; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As a side note, <?=$var;?> syntax should be avoided since short_open_tag is disabled in many PHP installations, and this was required to use that syntax until PHP 5.4.0
Depends on how You got the result form database it will be something like this:
<td><?=$result['book_title']?> </td>
<td><?=$result['id']?> </td>
<td><?=$result['isbn']?> </td>
<td><?=$result['visible_online']?> </td>
Or if You're using doctrine:
<td><?=$result->book_title?> </td>
<td><?=$result->id?> </td>
<td><?=$result->isbn?> </td>
<td><?=$result->visible_online?> </td>
You should read tutorial, things like that are in it :)
http://framework.zend.com/manual/en/zend.db.statement.html
...
<tbody>
<?php foreach($this->basicBwDetails as $result): ?>
<tr>
<?php foreach($result as $cell)?>
<td><?=$cell;?> </td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
...
Like this ?

Display multiple database fields into a PHP table loop

I've been struggling with this for around 4 hours now...
What I'm trying to establish is pretty simple, I have a news table, I want to display the title of the news, the content, and a read more link, I know how to loop through a table and force , but this won't work in my case, the table should look like this in the end:
<table>
<tr>
<td>header one</td>
<td>header 2</td>
<td>header 3</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td>text one</td>
<td>text two</td>
<td>text 3</td>
</tr>
<tr>
<td>read more</td>
<td>read more </td>
<td>read more</td>
</tr>
</table>
What I have so far in my php is a code that will generate the rows and columns, but I want them to be distributed just like the sample table above in order not to mess the alignment of the text and the read more link ...
Here's my php code :
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<?php while ($record = mysql_fetch_assoc($result)): ?>
<?php $style++ ?>
<td width="33%" valign="top">
<h6><?php echo $record['title'] ?></h6>
<div class="service-sum"><?php echo $record['content'] ?></div>
<div class="findout">> find out more</div>
</td>
<?php if ($style == 3): ?>
</tr>
<?php $style = 0; ?>
<tr>
<td>
<div style="height:30px;"></div>
</td>
</tr>
<tr>
<?php endif ?>
<?php endwhile ?>
This one is working fine, but i'm displaying the title and the content and the read more link in one column, these should be distributed into 3 for design purposes...
Any help would be much appreciated, I looked all over the net and I can't find a solution for that!
try using this alignment. The design is almost same as yours.
<table>
<tr>
<td>
<table>
<tr>
<td>header one</td>
</tr>
<tr>
<td>text one</td>
</tr>
<tr>
<td>read more</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>header 2</td>
</tr>
<tr>
<td>text 2</td>
</tr>
<tr>
<td>read more</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>header 3</td>
</tr>
<tr>
<td>text 3</td>
</tr>
<tr>
<td>read more</td>
</tr>
</table>
</td>
</tr>
</table>
if there are only 3 records, the following should be fine. if there are more, some changes should be made. Please check your php code.
<table>
<tr>
<?php while ($record = mysql_fetch_assoc($result)) { ?>
<td>
<table>
<tr>
<td><?php echo $record['title'] ?></td>
</tr>
<tr><td height = "30px;"></td></tr>
<tr>
<td><?php echo $record['content'] ?></td>
</tr>
<tr>
<td>> find out more</td>
</tr>
</table>
</td>
<?php } ?>
</tr>
</table>

Categories