I have a database with a few tables in it. The table i'm working on has 6 columns in it,
school_id, title, location, content, class_date and user_id
The school_id is unique (ranging from 1-20) and there are 4 different user_id (ranging from 1-4).....I'm working on a site which has a table that displays the information about the user and it's correspondence. I have a ReadMore link in the last column of the displayed table, my question is this, when I click the ReadMore link I want it to display the content field in the database based on which event is clicked.
For the login, the user is asked their user_id as their password.
//$T is called from a previous page which is the user_id or the password
$T = $_SESSION["ID"];
$INFO= $dbc->query("SELECT content FROM events where school_id='$T'");
$a = $INFO->fetch();
$b = $a['content'];
print_r($b);
THE ABOVE CODE IS HOW I TRY TO PRINT OUT THE CORRECT CONTENT BUT ONLY THE FIRST INSTANCE IS PRINTED OUT
THE BELOW CODE IS HOW THE TABLE IS DISPALYED ON MY SITE
<?php
$gold= $dbc->query("SELECT * FROM events WHERE user_id='$idcheck' ORDER BY event_dateLIMIT 10");
$x = $gold->fetchAll();
echo "<table border = '1'>";
echo "<tr>
<td> Event </td>
<td> Date of Event </td>
<td> Location </td>
<td> Event Information</td>
</tr>";
foreach ($r as $back) {
$title = $back['title'];
$eventdate = $back['class_date'];
$location = $back['location'];
echo "<tr>";
echo "<td>$title</td>";
echo "<td>$eventdate</td>";
echo "<td>$location</td>";
echo "<td>";
?>
<form action="displaycontent.php" method="post">
<input type="submit" value="Read More">
</form>
<?php
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
fetch method returns next found row from your statement. You have to iterate over your result to get all rows.
Something like
while ($row = $INFO->fetch()) {
$b[] = $row;
}
Then have a look at print_r($b). It should look like
array(0=>array('content'=>'Some content1'),
1=>array('content'=>'Some content2'), ....)
And your way of outputting the result should work.
Related
At the moment I got incomplete what I need, how can I continue to display the SELECT results in their respective tables?
As you can see in the example above - there is a image at the end of topic - the first dynamically generated table was populated with your right data. You can see by the title of the table that refers to the last column Event.
But in the other tables I can not generate the loop.
I'm in the right way ?
What I want to do:
I have two tables in MySQL one with names Events and other Tickets. They have in common a column named event with the same data. In HTML tables generated according to the number of rows in the Events table, I wanted to select the information from the Tickets table and put in their respective HTML tables, where the title of each is a column named event in the Events table.
<?php
include 'conection.php';
$tickets = $con->prepare("SELECT tickets.chair, tickets.name, tickets.event, events.event FROM tickets INNER JOIN events ON tickets.event = events.event");
$tickets ->execute();
$events = $con->prepare("SELECT event FROM events");
$events ->execute();
?>
<!doctype html>
<html>
<body>
<?php
foreach($events as $evt){
echo " <div class='box[]'>
<table border='1px'>
<thread>
<th> ".$evt['event']." </th>
<tr>
<th>chair</th>
<th>name</th>
<th>event</th>
</tr>
</thread> ";
while ($ingr = $tickets->fetch(PDO::FETCH_ASSOC)){
if(($tck['event']) === ($evt['evento'] )){
echo " <tbody> ";
echo " <tr> ";
echo "<td>" .$ingr['chair']. "</td>";
echo "<td>" .$ingr['name']. "</td>";
echo "<td>" .$ingr['event']. "</td>";
}}}
echo " </tr> ";
echo " </tbody> ";
echo " </table> ";
echo "</div>";
?>
</body>
</html>
Exemple
I cant display image yet, but this is what iam done for now.
Figure of the code executed
First, some cleaning up.
The <thead> tag is a "table head", not a "thread" :)
Using a <th> outside of the <tr> will not work; move the event name into a header (<h2> or something) above your table.
You are using the field name 'evento' when you selected the field 'event'.
To answer your main question: you are seeing only one set of tickets because you run through the whole set of tickets when you search for the right ones for your first event. This way, $tickets has been exhausted by the time you want to get the tickets for the second event. You could try putting the ticket information in a variable after selecting (using $tickets->fetchAll), then using that variable in your loop instead of the $tickets resultset.
I have a webpage where it shows the lists of Projects and the monitoring of its progress/finances for every quarter. As shown below:
As you can see, my table is comprises of Project Name and a lists of sub-title's underneath it. And a series of columns per each quarter. Thru PHP I was able to populate the list of sub-titles under the Project Name, which also being fetched from the server side. Here's the code:
$sql = mysqli_query($con," My SELECT Statement ");
$i=0;
while($row = mysqli_fetch_assoc($sql)){
$ptitle = $row['Title'];
$iname = $row['Item'];
if($i%1)
{
?>
<?php } else { ?>
<tr>
<?php } ?>
<td width="25%"><?php echo $ptitle; ?></td>
<td></td>
</tr>
<tr>
<td><?php echo "<ul style='list-style-type: none;'><li>".nl2br($iname)."</li></ul>"; ?></td>
<td contenteditable="true" name="v1"></td>
Note: ptitle = ProjectName and iname = Semi-title underneath the Project's name.
Now, as you can see, the Project Name column literally "conquer" a single row on the left. Yet, the rows under the column of each quarter, should have its own separately, and must be parallel to the every sub-title underneath the Project Name. (Please refer to the image above for this) the only problem am encountering is... how can I make an editable row from inside a row, without affecting mysqli result? coz basically my table right now is kinda look like this:
Anyone who's more experience on this? I need your help.
PS: ...and oh! You might be wondering why do I include JSON in the title? It is because, I originally use JSON for editing those table rows before I even use mysqli_fetch_array. But when I include the results of the array inside the <table> tag, everything's changed and JSON is no longer working. So as of now, I am force to do it manually, meaning typing each <td contenteditable=true> in all of those rows. Yet, its not the desired output since I need another row within an existing row. Ideas? Anyone?
Figure Two:
Figure Three:
Count the number of lines in $iname, and then use a loop to create that many rows of contenteditable cells. You can also use this in the rowspan attribute of the <td> containing the title and subtitles.
$rows = substr_count($iname, "\n") + 1;
for ($i = 0; $i < $rows; $i++) {
echo "<tr>";
if ($i == 0) { ?>
<td rowspan='<?php echo $rows;?>' width='25%'><?php echo $ptitle . "<br>" . nl2br($iname);?></td>
<?php }
?>
<td contenteditable="true" name="v1"></td><td contenteditable="true" name="v2"></td>...
</tr>
<?php }
DEMO
so, what i wanted to do is show the total clicks per Category id of the items only for 20 items order by the highest total clicks per day.
now i am using hard code and the result have to looks like this
so, if the item id and total clicks already fill up the
20columns (for item id and total clicks) + 2 for the tittle so means
22columns
it has to move to next row.
because i am reffering to my db, so i am using loop to create the table, and when i doing that way, i am getting this result....
the result will keep showing until the end in the left side. thats very hard to read for report purposes. so i wanted the result looks like the first figure that i've uploaded.
here is what i am doing now
include "Con.php";
//get the value from Get Method
$CatidValue = $_GET['CatIds'];
//(The date format would looks like yyyy-mm-dd)
$DateFrom = $_GET['DateFrom'];
$DateTo = $_GET['DateTo'];
//select the CatID
$SqlGet= "Select CatId from try where CatId = $CatidValue";
$_SqlGet = mysqli_query($connection,$SqlGet);
$TakeResultGet = mysqli_fetch_array($_SqlGet);
$CatIdTittle = $TakeResultGet ['CatId'];
echo"
For Category Id : $CatIdTittle
<br>
";
//For Loop purpose and break the value
$explodeValueFrom = explode("-",$DateFrom);
$explodeValueTo = explode("-",$DateTo);
$DateValueFrom = $explodeValueFrom[0].$explodeValueFrom[1].$explodeValueFrom[2];
$DateValueTo = $explodeValueTo[0].$explodeValueTo[1].$explodeValueTo[2];
//Loop through the date
for($Loop=$DateValueFrom; $Loop <= $DateValueTo; $Loop++){
$YearLoop= substr($Loop, 0,4);
$MonthLoop =substr($Loop, 4,2);
$DayLoop = substr($Loop, 6,2);
$DateTittleValue = $YearLoop."-".$MonthLoop."-".$DayLoop;
$trValue = "<th class='tg-amwm' colspan='2'>$DateTittleValue</th>";
echo"
<table class='tg'>
<tr>
$trValue
</tr>
";
echo"
<table class='tg'>
<tr>
<td class='tg-yw4l'>Items Id</td>
<td class='tg-yw4l'>Total Clicks</td>
</tr>
";
//to get the item id and total clicks
$SqlSelect = "select `Item Id`,`Total Clicks`,Day from try where CatId = $CatidValue and Day = '$DateTittleValue' ORDER BY `try`.`Total Clicks` DESC limit 20";
$_SqlSelect = mysqli_query($connection,$SqlSelect);
foreach ($_SqlSelect as $ResultSelect) {
$Day = $ResultSelect['Day'];
$ItemId = $ResultSelect['Item Id'];
$TotalClicks = $ResultSelect['Total Clicks'];
echo"
<tr>
<td class='tg-yw4l'>$ItemId</td>
<td class='tg-yw4l'>$TotalClicks</td>
</tr>
";
}
}
?>
You dont need to declare your trValueTitle in the loop. Plus you need to concatenate it in your echo, try this :
$trValueTittle ="1"."<th class='tg-amwm' colspan='2'>$DateTittleValue</th>" ;
echo"<table class='tg'>";
for($loopForTr=1; $loopForTr<=3; $loopForTr++){
echo"<tr>". $trValueTittle ."</tr>";
}
I don't know what you want to do with the "1", but I think this is what you want to do:
echo "<table class='tg'>";
for($loopForTr=1; $loopForTr<=3; $loopForTr++){
$trValueTittle ="<th class='tg-amwm' colspan='2'>$DateTittleValue</th>";
echo"
<tr>
$trValueTittle
</tr>";
}
echo "</table>";
$trValueTittle ="1"."<th class='tg-amwm' colspan='2'>$DateTittleValue</th>" ;
echo"<table class='tg'>
<thead>
<tr>";
for($loopForTr=1; $loopForTr<=3; $loopForTr++){
echo $trValueTittle;
}
echo"</tr>
</thead>
</table>";
#Mantello, good answer, but i think it's better to keep out the "tr" from the loop, cause usually you don't want to place your tableheads (th) in different rows.
#Rax your Code won't work. You'll recive an error. You can't output a Variable the way you did in your echo.
echo"<tr> '.$trValueTittle.' </tr>";
would be fine. But there's also no need to define your variable inside the for loop. This way you slow up your script.
I have this table with checkboxes, my idea is to be able to delete the rows where the checkboxes have been checked.
With the help of Charaf jra, I was able to POST the uniqID of the row so I could DELETE it using mysql query on my delete.php page.
My problem now is in order to pass the uniqID I had to add it on the table, which doesnt look good. I mean, I dont want the ID number showing on the table. I have been reading on how to hide it, but none of the explanations I have read apply to my case.
Here is my code updated:
if ($arch = $pdo->prepare("SELECT name, age, uniqID FROM table WHERE id = ?")) {
$arch ->execute(array($id));
$data = $arch->fetchAll();
echo '<div class="coolTable" ><form method="post" action="delete.php"><table><tr><td>Name</td><td>Age</td><td>Check</td></tr>';
foreach ($data as $row){
echo '<tr>';
foreach ($row as $col){
$col=nl2br($col);
echo '<td>'.$col.'</td>';
}
echo '<td><input type="checkbox" name="checkbox[]" value="'.$col.'" id="checkbox"></td>'; //this captures the column ID so I can pass it through the `POST`
echo '</tr>';
}
echo '</table><input type="submit" value="Delete Selected"/></form></div>';
}
This works perfect. The only big problem is that I dont want the uniqID to be shown. Can anyone tell me how to hide it from the table and still be able to pass it through the POST?
if ($arch = $pdo->prepare("SELECT name, age, uniqID FROM table WHERE id = ?")) {
$arch ->execute(array($id));
$data = $arch->fetchAll();
echo '<div class="coolTable" ><form method="post" action="delete.php"><table><tr><td>Name</td><td>Age</td><td>Check</td></tr>';
foreach ($data as $row){
echo '<tr>';
echo '<td>'.nl2br($row['name']).'</td>';
echo '<td>'.nl2br($row['age']).'</td>';
echo '<td><input type="checkbox" name="checkbox[]" value="'.nl2br($row['uniqID']).'" id="checkbox"></td>'; //this captures the column ID so I can pass it through the `POST`
echo '</tr>';
}
echo '</table><input type="submit" value="Delete Selected"/></form></div>';
}
PHP arrays can be used as dictionaries (if you've seen python or ruby). PDO returns an array of pairs key-value, where your key is the name your column and the value, the value of your column. So you can access those values via
iteration (foreach)
iteration (index - direct access if you know the position of your element inside the array)
key (direct access without knowing the position of your element inside the array)
Assuming you want to loop the columns in each row, as opposed to manually echoing them as alkis suggests, get the results as an associative array and then unset $row['uniqID'];
if ($arch = $pdo->prepare("SELECT name, age, uniqID FROM table WHERE id = ?")) {
$arch ->execute(array($id));
$data = $arch->fetch(PDO::FETCH_ASSOC);
echo '<div class="coolTable" ><form method="post" action="delete.php"><table><tr><td>Name</td><td>Age</td><td>Check</td></tr>';
foreach ($data as $row){
if ( isset($row['uniqID']) ) {
unset($row['uniqID']);
}
echo '<tr>';
foreach ($row as $col){
$col = nl2br($col);
echo '<td>'.$col.'</td>';
}
echo '<td><input type="checkbox" name="checkbox[]" value="'.$col.'" id="checkbox"></td>'; //this captures the column ID so I can pass it through the `POST`
echo '</tr>';
}
echo '</table><input type="submit" value="Delete Selected"/></form></div>';
}
I have various tasks under various departments. The department name and id come as table header (<th>). I have displayed them.
Now I query the tasks and if the task belongs Dept A I want it under <th id='A'> and if task belogs to Dept B it should be under <th id='B'>.
My problem is that some depts may not have task for this particular row and so that cell should be empty. The next cell may have value. So how to tell html to put an empty cell when necessary ie, I want to do something like this:
if(th id == 'DeptA' and resultSet.deptId = 'DeptA')
then <td> resultSet(i); </td>
else <td> </td>
I am a bit new to HTML and PHP. All help is greatly appreciated.
How about this:
<?php
echo '<table>';
foreach(...){
echo '<tr><td>';
if(th id == 'DeptA' && resultSet.deptId == 'DeptA'){
echo resultSet(i);
}
echo '</td><td>';
if(th id == 'DeptB' && resultSet.deptId == 'DeptB'){
echo resultSet(i);
}
echo '</td></tr>';
}
echo '</table>';
?>
<table> - starts the table
<tr> - is a table row
<td> - is a table column