Hi I am looking to add a Show More / Load More button to this For Each loop.
I am getting data from an API through JSON and displaying it in 3 columns.
I would like to limit it to say 3 rows of three, then perhaps use another row with only 1 td with a button centred with a GET parameter adding more to a Limit variable?
Here is my code how would be the best way to do this? Many thanks
$output = json_decode($output);
curl_close($ch);
$i = 0;
echo '<table border="0" align="center" padding-top="10px" padding-right="10px" padding-left="10px" padding-bottom="10px" style="width: 100% ">';
echo '<tr >';
foreach($output as $template) {
echo '<td align="center"><br /><br /><b>' . '<font color="#0083C1" size="5" face="sans-serif">'.$template->template_name .'</font></b><br />';
echo '<ul class="img-list"><li><img src="'. $template->thumbnail_url.'" height="250" width="370">'.'<span class="text-content"><b>'.'Preview Website'.'</b></span>'.'</li></ul>';
echo '<form method="GET" value="/websites/choose-your-website.php">';
echo '<input type="hidden" name="template_id" value="' . $template->template_id . '">';
echo '<input type="email" name="email" placeholder="Enter Your Email and Click Below" required" align="center" style="width: 250px">'.'<br />';
echo '<button type="submit" align="center" class="button">Choose Site</button><br />';
echo '</form></td>';
$i++;
if($i == 3) {
echo '</tr>'.'<tr>';
$i = 0;
}
}
echo '</tr>';
echo '</table>';
The file is in PHP with JSON data pulling information from an API displaying in a PHP table
Related
I have a table that displays every unapproved leave, it looks like this:
For each unapproved there will be a new row, so another checkbox, but the name of the checkbox will be the same or slightly different(name="approve1"). Personally I don't think that's good practice...
Also the evaluation isn't really good, because it has to go into the database.
<form role="form" method="post" action="" id="form">
<h2><?php echo $title ?></h2>
<?php
$page = date('F');
$page = lcfirst($page);
echo "<p><a href='$page.php'>Back</a></p>";
?>
<hr>
<?php
if($i == 0){
echo "<h3>No unapproved leaves.</h3>";
}else{
echo '<table class="table table-striped table-hover">';
echo '<thead>';
echo '<th>';
echo 'Start';
echo '</th>';
echo '<th>';
echo 'End';
echo '</th>';
echo '<th>';
echo 'Employee';
echo '</th>';
echo '<th>';
echo 'Approve';
echo '</th>';
echo '</thead>';
echo '<tbody>';
for($j =0; $j < $i; $j++){
echo '<tr>';
echo '<td>';
echo $unapproved[$j]['start'];
echo '</td>';
echo '<td>';
echo $unapproved[$j]['end'];
echo '</td>';
echo '<td>';
$id = $unapproved[$j]['employee_FK'];
$result = mysql_query("select name, surname from employee where employee_ID = $id");
while ($row = mysql_fetch_assoc($result)) {
$employee[] = $row;
}
echo $employee[0]['name'], " ", $employee[0]['surname'];
echo '</td>';
echo '<td>';
echo '<input type="checkbox" name="approve" value="1" id="approve">';
echo '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
}
?>
<input type="submit" name="submit" value="Submit" class="btn" >
</div>
</div>
</form>
Can anyone think of a better, easier solution?
The practice for tabular data in most frameworks is to name them Model[id][field], so in your example it would look like "<input type='checkbox' name='leave[$j][approved]' value='1' id='leave_$j_approved'>".
Then you can easily access your data by iterating the post data like so:
foreach($_POST['leave'] as $id => $values) {
}
use checkbox name like this
<input type="checkbox" name="approve[]" value="<?php $your_row_id; ?>" id="approve">
here $your_row_id means unique id of a row in database.
And in post you will get an array named approve with all the selected values.
Parse that array using foreach.
You can generate checkboxes using a array notation in the "name" attribute.
<input type="checkbox" name="approve[]" value="1" id="approve">
After on send, you can recover values using array notation $_GET['approve'][0], ...
You can transfer arrays via HTTP :
echo '<input type="checkbox" name="approve[]" value="', $id, ']" id="approve">';
Now $_GET['approve'] will be an array which only contains values of checked boxes.
You can also specify the key associated with the value:
echo '<input type="checkbox" name="approve[', $id, ']" value="', $id, ']" id="approve">';
http://i.stack.imgur.com/TyC6U.png
I have something like this and when a person checked a checkbox and then click the button Save, it would save the checked box and the next time the person see this table it would appear the table with all the checkboxs that the person checked or unchecked last time. How can I do that?
The months are in one array, check my code:
<?php
$results = mysql_query("SELECT * FROM utilizadores");
$month = array("January","February","March","April","May","June","July","August","September","October","November","December");
echo '<form id="form" method="post" action="">';
echo '<table border="1" align="center">';
echo '<tr class="d0">';
echo '<td align="center" height="30" width="400"><strong>Nome</strong></td>';
for($i=0;$i<=11;$i++)
{
echo '<td align="center" width="80"><strong>'.#$month[$i].'</strong></td>';
}
echo '</tr>';
$rowColors = Array('#ffffff','#ffffcc'); $c= 0;
while($row = mysql_fetch_array($results)) {
echo '<tr style="background-color:'.$rowColors[$c++ % count($rowColors)].';">';
echo '<td align="center" height="30" width="400">' . $row['nome'] . "</td>";
for($i=0;$i<=11;$i++) {
echo '<td align="center" width="80"><input type="checkbox" name="asd" /></td>';
}
}
echo "</tr>";
echo "</table>";
echo "<br>";
echo '<input name="submit" type="submit" value="Save" />';
echo '</form>';
?>
<br />
</body>
</html>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
This code works and is fine, but I was wondering if there was an easier way to code something like this?
<?php
while($rows=mysql_fetch_array($result)){
$pid=$rows['id'];
$d = $rows['date'];
echo '<style>tr, td {border:0;} #dat{text-align:right;} #co {background-color:#33B533;} </style>';
echo '<div id="postd">';
echo '<table id="mdata">';
echo '<tr>';
echo '<td>';
echo '<a href="profile.php?name=';
echo $rows['name'];
echo '">' . $rows["name"] . '</a>';
echo '</td>';
echo '<td id="dat">';
echo $result7 = nicetime($d);
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
echo $rows['post'];
echo '</td>';
echo '<td id="dat">';
echo '<form method="post" action="delete.php">';
echo '<input type="hidden" value="';
echo $rows['id'] . '" name="id" />';
echo '<input id="sub" type="submit" name="delete" value="Delete" />';
echo '</form>';
echo '<form method="post" action="edit.php">';
echo '<input type="hidden" value="';
echo $rows['id'] . '" name="id" />';
echo '<input id="sub" type="submit" name="edit" value="Edit" />';
echo '</form>';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
$sql9 = "SELECT * FROM `like` WHERE postid='$pid' AND userid='$userid'";
$result9=mysql_query($sql9);
if($result9){
echo '<form method="post" action="like.php">';
echo '<input type="hidden" value="';
echo $rows['id'] . '" name="id" />';
echo '<input type="hidden" value="';
echo $userid . '" name="nid" />';
echo '<input id="sub" type="submit" name="like" value="Like" />';
echo '</form>';
}
echo '</td>';
echo '<td id="dat">';
if ($rows['like'] == 1) {
echo $rows['like'] . " Like";
} else if ($rows['like'] == 0) {
echo "";
} else {
echo $rows['like'] . " Likes";
}
echo '</td>';
echo '</tr>';
echo '</table>';
echo '</div>';
$sql2="SELECT * FROM comments WHERE postid='$pid' ORDER BY date ASC";
$result2=mysql_query($sql2);
while($rows2=mysql_fetch_array($result2)){
$c = $rows2['date'];
echo '<table id="co" border="1px solid black" width="250px" align="center">';
echo '<tr>';
echo '<td>';
echo $rows2['name'];
echo '</td>';
echo '<td id="dat">';
echo $result4 = nicetime($c);
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
echo $rows2['comment'];
echo '</td>';
echo '</tr>';
echo '</table>';
}
echo '<form action="addcomment.php" method="post">';
echo '<table id="co" border="1px solid black" width="250px" align="center">';
echo '<tr>';
echo '<td>';
echo '<textarea id="target" type="text" name="com" placeholder="Comment"></textarea>';
echo '</td>';
echo '<td>';
echo '<input type="submit" name="submit" value="Post">';
echo '</td>';
echo '</tr>';
echo '</table>';
echo '<input type="hidden" value="';
echo $rows['id'] . '" name="id" />';
echo '<input type="hidden" name="name" value="Jon" />';
echo '</form>';
echo '<br/>';
}
?>
Basically I've created a Facebook type system. This spools the posts down in the order they are posted in the database. Check it out at http://bockhorst.comeze.com/Wall/wall.php Its still a work in progress. Also does anyone know a php compressor?
The more efficient way to do this is to use a proper development framework that provides some structure and cohesion to your code. What you have here is a stew of SQL, HTML, and code. It's extremely hard to maintain an application written like this.
Additionally you're using the woefully out of date mysql_query interface. Please, don't. It's terrible. You really should be using an ORM to handle your routine database interfacing, something like Doctrine or Propel. These make it easier to compose the queries correctly and deal with the data instead of being stuck trying to render all your application logic directly in SQL.
If you're dead-set on doing SQL by hand, PDO is the way to go.
I have a PHP form that updates records in a database. It looks something like this.
//update a record
$query1 = 'UPDATE mytable SET name="'.$name.'", description="'.$desc.'",
img="'.$img.'" WHERE id="'.$id.'" ';
mysqli_query($con,$query);
//get record set
$query2 = 'SELECT * FROM mytable WHERE id="'$id'"';
$result = mysqli_query($con,$query2);
echo '<form action="my-update-page.php" method="post">';
//table heading row
echo '<table width="1000" border="1" cellspacing="0" cellpadding="1">';
echo '<tr>';
echo '<td>ID</td>';
echo '<td>NAME</td>';
echo '<td>description</td>';
echo '<td>Image</td>';
echo '</tr>';
//display data
while($row = mysqli_fetch_array($result))
{
echo '<input type="hidden" name="id" value="' . $row['id'] . '" />';
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td><input type="text" name="name" value="'. $row['name'].'" /></td>';
echo '<td><textarea name="description">'.$row['description'].'</textarea></td>';
echo '<td><input type="text" size="3" name="img" value="'. $row['img'].'"/>;
echo 'Upload Image</td>';
echo '</tr>';
}
//closing tag for table
echo '</table>';
echo '<br /><input type="submit" value="submit" /></form>';
I want my upload.php page to open in a popup where the user can upload the image. I'm pretty sure I can manage doing that. Where I get stuck is after the file is uploaded, I want the popup to close and file name to show in the form input.
Modify to reflect your names, but window.opener is the link to the other window. At that point, access any elements the same way.
window.opener.forms['myform'].elements['fileinput'].value = nameOfFile;
window.close();
As the comment above says, use AJAX to do the file post.
What I'm doing is trying to set up a page where users can see their created tools and delete them whenever they want. I'm querying three different tables and putting the results into an array. Once those values are in an array, a foreach loop goes through and populates a table with all the information in a table, like so:
$counter = 1;
echo '<table>'
foreach ($recent_saved_tools as $key => $value) {
echo '<tr name="item'.$counter.'">';
echo '<td>';
echo '<input type="hidden" name="tablename" value="'.$value['table'].'" />';
echo '<input type="hidden" name="tabledelete" value="'.$value['delete'].'" />';
echo '<input type="hidden" name="tableidfield" value="'.$value['idfield'].'" />';
echo '<input type="hidden" name="tableid" value="'.$value['id'].'" />';
//code to display the tool name and link
echo '<a style="text-decoration:none;" href="'.WEBSITE.'tools/'.$value['URL'].'?saved_data_id='.$value['id'].'">'.$value['display'].'</a><br />';
echo date("m/d/Y H:i:s", $key).'<br />';
echo '</td><td>';
//code to display the delete button
echo ' <input class="cssformbutton bluebutton" type="button" name="delete" id="deletebtn'.$counter.'" value="Delete" /><br /><br /><br /><br /></td>';
$counter ++;
}
echo '</table>';
The problem is whenever I run the SQL query, no matter what button I click it always takes the values from the last table row. I know it has something to do with the way they're named (multiple elements have the same name) but I'm at a loss on how to fix this. Any help would be appreciated.
Here's the query I'm using to delete the item:
$query = 'UPDATE '.$value['table'].' SET '.$value['delete'].' = 1 WHERE '.$value['idfield'].' = '.$value['id'];
$sql->query($query);
EDIT: added delete code
Every row has some inputs which are posted to the server. They have the same names - for every row. You could change it like this:
echo '<input type="hidden" name="tablename'.$counter.'" value="'.$value['table'].'" />';
Then you can use $_POST['tablename'.$rownr] in your delete code.
Doesn't look like you're closing the anywhere...
What results return from the query?
What output are you expecting?
try making it a for loop in stead and adding the variable to the end.
$array;
$counter = count($array);
echo '<table>'
for($i = 0; $i < $counter - 1; $i++) {
echo '<tr name="item'.$i.'">';
echo '<td>';
echo '<input type="hidden" name="tablename" value="'.$value['table']. $i'" />';
echo '<input type="hidden" name="tabledelete" value="'.$value['delete']. $i'" />';
echo '<input type="hidden" name="tableidfield" value="'.$value['idfield']. $i'" />';
echo '<input type="hidden" name="tableid" value="'.$value['id'].'" />';
//code to display the tool name and link
echo '<a style="text-decoration:none;" href="'.WEBSITE.'tools/'.$value['URL'].'?saved_data_id='.$value['id'].'">'.$value['display'].'</a><br />';
echo date("m/d/Y H:i:s", $key).'<br />';
echo '</td><td>';
//code to display the delete button
echo ' <input class="cssformbutton bluebutton" type="button" name="delete" id="deletebtn'.$counter.'" value="Delete" /><br /><br /><br /><br /></td>';
}
echo '</table>';