How For Loop is generating dynamic table? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My teacher has shown us the following script:
<table border="1" width="600" height="600" cellspacing="0" cellpadding="0">
<?php
$iNew=1;
echo '<tr>';
for ($iCounter=1;$iCounter<=15;$iCounter++)
{
echo '<td>'.$iCounter.'</td>';
if ($iNew==3) {
echo '<tr></tr>';
$iNew=0;
}
$iNew++;
}
echo '</tr>';
?>
</table>
Here, the <tr> tag is inside the <td> tag. In normal HTML this does not work because cell tags must be inside row tags, but when run in the above php, the appropriate number of columns appear. How is this script actually working?

It should be
if ($iNew==3)
{
echo '</tr><tr>';
$iNew=0;
}
$iNew++;
You had TRs reversed, creating a new row and ending it immediately instead of ending the previously created row and then starting a new one!

Related

How to do inline styling in html/php? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am working on a website in which I want to do inline styling inside php tags.
<?php
if($data['item']->hello_world!=null)
{
echo "policy:";
echo strtolower($data['item']->hello_world->name);
echo "<br>";
echo strtolower($data['item']->hello_world->description);
}
?><?php
if($data['item']->hello_world!=null)
{
echo "policy:";
echo strtolower($data['item']->hello_world->name);
echo "<br>";
echo "<br>";
echo strtolower($data['item']->hello_world->description);
}
?>
The data which I want to style coming from the php code is:
echo strtolower($data['item']->hello_world->name);
Problem Statement:
I am wondering what changes I should make in the php code so that I am able to do styling for the above mentioned line.
To inline style the element:
echo '<span style="color: red">' . strtolower($data['item']->hello_world->name) . '</span>';

Looking for proper way to inject HTML table into PHP code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Let me start by saying that i'm sorry if this is a complete noob question.
while using phpStorm, I have an array of $teams and by invoking foreach loop I tried to put each of the array section into an HTML table
here is what I did so far
I am getting cannot use [] for reading, I can't find an answer how to do that properly, thx in advance.
<table>
<tr>
<th>Team</th>
<th>ID</th>
</tr>
<?php
include dirname(__FILE__) . "/stats.php";
global $teams;
foreach ($teams['data'] as $team) {
echo "<tr>" ;
echo "<td> $team['name']</td>" ;
echo "<td> $team ['id']</td> " ;
echo "</tr>";
}
?>
</table>
To get the string substitution working correct, you have to suround it with {} if it's something more complex than a simple var - especially array access.
echo "<td> {$team['name']}</td>";

Use PHP to highlight cells in table depending on entry to database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to display a timetable of sorts on my webpage. I need to use PHP to highlight table cells depending on whether they have an entry to the database for todays date. I currently have a table with rooms across the top and times down the side as below:
echo "<table border-1>
<tr>
<th></th>
<th>B-12</th>
<th>G.05</th>
<th>G.06</th>
<th>1.04</th>
<th>1.05</th>
<th>1.06</th>
<th>2.04</th>
<th>2.05</th>
<th>2.06</th>
<th>3.04</th>
<th>3.05</th>
<th>3.06</th>
</tr>
<tr>
<th>9.00</th>
<td></td>
</tr>
<tr>
<th>9.30</th>
<td></td>
</tr>
<tr>
<th>10.00</th>
<td></td>
</tr>
<tr>
<th>10.30</th>
<td></td>
</tr>";
I need to read entries to my database and highlight the corresponding cell, in the correct room column and in the correct time row, based on the entry date,
like this
Is there a way to do this in PHP? Am I going about this all wrong? I am new to PHP so am not entirely sure what is the best method to achieve what I need.
I have searched everywhere for a solution to this but have found none that answer my question. If anyone has any idea or could advise me on the best method I would really appreciate it.
I am still a bit confused if you question is more about how to interact with mysql or how to display occupancy/availability in each room.
I assume it is the latter. I also assume you know how to get your data from mysql and you just need a way on how to display the occupancy.
<?php
$roomsAvailability = [
'1' => ['9:30', '10:30'],
'2' => ['10:00'],
'3' => ['9:00', '11:00'],
];
$times = ['8:30', '9:00','9:30', '10:00', '10:30', '11:00'];
?>
<style>
table, th, td {
margin:5px;
border: 1px solid black;
}
.available {
background-color: lightblue;
}
</style>
<table>
<tr><th> </th><th>g1</th><th>g2</th><th>g3</th></tr>
<?php
foreach($times as $time)
{?>
<tr>
<?php
echo "<td>$time</td>";
foreach($roomsAvailability as $roomNumber => $roomAvailability)
{
if(in_array($time , $roomAvailability))
{
echo "<td class='available'> </td>";
}
else{
echo "<td class='notAvailable'> </td>";
}
}
?>
</tr>
<?php
}
?>
</table>
In the script above you can see how i represent the availability in a variable for the rooms (lets assume you get this array from the database). Also the times (for instance times of business/slots) are also in the db.
In general in a db you would have relations and more elaborate things that I have skipped from the answer.
The script displays a table similar to the one you added in your question with cells where the room is allocated marked with a lightblue background.
You can copy and run it on PHP Fiddle to see the output.
Please let me know if it is what you were looking for

PHP/SQL doesn't fetch all children from parent [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I created this code for a dropdown navigation menu. I have 2 tables in my database, one for parentitems and the other one for childitems. The parents getting out correctly, but the childitems won't.
The problem is, I only get one parent and one child at a time, or I get totally nothing.
Thanks in advance!
My code:
<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db('navigation',$con);
$query="select * from nav";
$run=mysql_query($query);
while($row=mysql_fetch_array($run)){
$m_id=$row['m_id'];
$m_title=$row['m_title'];
$child_query="select * from nav_child where parent_id='$m_id'";
$run_child=mysql_query($child_query);
while($row_child=mysql_fetch_array($run_child)) {
$child_id=$row_child['nav_id'];
$child_title=$row_child['child_title'];
echo"<ul>
<li><a href='menu.php'>$m_title</a>
<ul>
<li><a href='menu.php'>$child_title</a></li>
</ul>
</li>
</ul>";
}
}
?>
You need to split your html
while(mainquery) {
echo '<ul>' <-----note the location
while (subquery) {
echo '<li>subquery 1 stuff</li>'
}
echo '</ul>' <-----note the location
}
You're outputting it entirely in your subquery section, so EVERY child row gets its own complete <ul><li>...</li></ul> tag set.

search results displaying the same thing [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a search bar and a sql database with 3 movies in
Name:Star Wars: Description: Contains the word group
Name:301: Description: Contains the word group
Name:destroyer: Description: Contains the word group
BUT when ever I search for the word "group"
I only get one result usually:
if i search for group star wars will appear 3 time.
also i know its vulnerable to sql injection but at the moment that is not an issue
here is my php code:
if(!isset($_POST['search']))
{
header("Location:index.php");
}
$search_sql="SELECT * FROM php_item WHERE Name LIKE '%".$_POST['search']."%' OR Description LIKE '%".$_POST['search']."%'";
$search_query = mysql_query($search_sql);
if(mysql_num_rows($search_query)!=0)
{
$search_rs= mysql_fetch_assoc($search_query);
}
?>
</p>
<p> Search Results</p>
<?php
if(mysql_num_rows($search_query) != 0){
do{ ?>
<p>
<?php echo $search_rs['Name']; ?>
<?php }while($searchr_rs=mysql_fetch_assoc($search_query));
} else {
echo "No Results Found";
}
?>
If this is your code, there is a typo in the while. It should be $search_rs=mysql_fetch_assoc($search_query). You have an extra 'r'.
Good Luck!

Categories