I am trying to display some images and radio buttons in a two-column table format.
It should start a new row after every two images, instead, the row is ended even though the counter is less than 2 ($i == $c)
I am getting:
<td align="center" bgcolor=#CCCCCC>
<img src=http://web.com/7534560_2.jpg width='200' height='125' />
<br>Primary Image<input type="radio" name="primary" id="primary"
value=http://web.com/7534560_2.jpg />
**</td></tr>**
<td align="center" bgcolor=#CCCCCC>
<img src=http://web.com/7534560_3.jpg width='200' height='125' />
<br>Primary Image<input type="radio" name="primary" id="primary"
value=http://web.com/7534560_3.jpg />
Instead of the expected:
<td align="center" bgcolor=#CCCCCC>
<img src=http://web.com/7534560_2.jpg width='200' height='125' />
<br>Primary Image<input type="radio" name="primary" id="primary"
value=http://web.com/7534560_2.jpg />
**</td>**
<td align="center" bgcolor=#CCCCCC>
<img src=http://web.com/7534560_3.jpg width='200' height='125' />
<br>Primary Image<input type="radio" name="primary" id="primary"
value=http://web.com/7534560_3.jpg />
Here is the code:
<?php
$iq = 2; // first image index offset
$c = '2'; // number of cells in a row
$i = 0; // counter
while ($image_qty + 1 > $iq) {
if ($i==0) {
echo '<tr>';
}
echo '<td align="center" bgcolor=#CCCCCC>';
$image_url = substr($image_url, 0, -4);
$image_url = $image_url . "_$iq" . ".jpg";
echo "<img src=$image_url width='200' height='125' /><br>";
echo 'Primary Image<input type="radio" name="primary" id="primary" value=' . $image_url .' />';
echo '</td>';
$image_url = $image_url_org; // reset image value
$iq++; // increment image number
$i++; // increment cell counter
if ($i == $c) // if number of cells at 2 end row
{
echo '</tr>';
$i = 0;
}
if ($i > 0 ) // if cells remaining less than 2
{
echo '</tr>';
}
}
?>
Related
previously i have posted one post about the cart page call shopcart.php but it seems that no one really can answer it. whenever i input new quantity and click change quantity button, it seems that the new amount cannot be got and the button has no action, (i really have tried really really hard to find the solution for three wks , uptil now still really cannot find a solution of where the problem is) hope everyone can give me a warm hand to find the problem for me or hints to me
<?php
error_reporting(0);
session_start();
header('Content-type: text/html; charset=utf-8');
include_once('../../xajax_core/xajax.inc.php'); // 引用 xajax
$xajax = new xajax(); // 建立 xajax 物件
// 註冊回應函式
$chaObj=$xajax->registerFunction('change_quantity');
$chaObj->useSingleQuote();
$chaObj->addParameter(XAJAX_FORM_VALUES,'form1');
$xajax->processRequest(); // 處理回應
$xajax->printJavascript();
//---------------------- 自訂函式區 ---------------------
// 負責更改商品數量的回應函式
function change_quantity($form) {
$objResponse = new xajaxResponse(); // 建立回應物件
// 表單中的 Checkbox 屬性為 p_id, 若有勾選, 其值才會傳入
// 所以檢查表單欄位中有 'p_id' 資料, 表示使用者有勾選產品
// 此時才做後續處理
if(isset($form['p_id'])) {
// 逐筆處理每一個被勾選的產品
foreach($form['p_id'] as $p_id) {
// 根據產品的訂購數量決定處理方式
if($form['qua'][$p_id]=='0') {
// 若訂購數量被設為 0, 則將此產品從購物車中移除
unset($_SESSION['cart'][$p_id]);
if(count($_SESSION['cart'])==0) { // 若購物車變成空的
$objResponse-> // 則關閉視窗
script('alert("cart has no items");window.close();');
unset($_SESSION['cart']);
}
else { // 若購物車中還有其它商品
// 將被刪除的產品從網頁中移除
$objResponse->remove($p_id);
// 更新購物車中的金額總計
$objResponse->assign('total','innerHTML',gettotal());
}
}
else if ($form['qua'][$p_id]>0) {
// 若設為大於 0 的值, 則將之設為購物車中的新數量
$_SESSION['cart'][$p_id]['quantity'] = $form['qua'][$p_id];
// 因已修改數量, 所以要更新購物車中的小計及總計金額
$objResponse->assign('sub[' . $p_id . ']', 'innerHTML',
$_SESSION['cart'][$p_id]['quantity'] *
$_SESSION['cart'][$p_id]['price']);
$objResponse->assign('total','innerHTML',gettotal());
}
else {
// 若被設為負值, 則將之回復原本的值
$objResponse->assign('qua[' . $pid . ']', 'innerHTML',
$_SESSION[$pid]['quantity']);
}
}
}
return $objResponse; // 傳回回應物件
} // 回應函式 change_quantity() 結束
// 計算總金額的函式
function getTotal() {
$total = 0;
foreach($_SESSION['cart'] as $p_id => $item)
$total += ($item['quantity'] * $item['price']);
return $total;
}
//---------------------- 購物車 HTML ---------------------
// 目前購物車中有商品, 程式才取出購物車內產品並顯示於網頁
if( !isset($_SESSION['cart']) ) { // 使用者尚未購物
echo "<script>alert(\"you haven't chosen any products\");" .
"window.close();</script>";
exit();
}
?>
<title>購物車內容</title>
<!-- -------------------- 用戶端 JavaScript ------------------ -->
<script type="text/javascript">
// 勾選或取消產品清單中所有產品 checkbox 的函式
function select_all(formName, elementName, selectAllName) {
elem = document.forms[formName].elements[elementName];
if(!elem) // 若找不到元素
return;
else if(elem.length!= null) // 若網頁列出多個產品 (elem 是陣列)
for(var i = 0; i < elem.length; i++)
elem[i].checked =
document.forms[formName].elements[selectAllName].checked;
else
elem.checked =
document.forms[formName].elements[selectAllName].checked;
}
// 將勾選的產品之訂購數量設為0, 再產生非同步要求
function settozero(formName, elementName) {
elem = document.forms[formName].elements[elementName];
if(!elem) // 若找不到元素
return;
else if(elem.length!= null) { // 若網頁列出多個產品 (elem 是陣列)
for(var i = 0; i < elem.length; i++)
if(elem[i].checked) {
var qua =
document.getElementById("qua[" + elem[i].value + "]");
var subtotal = document.getElementById("sub[" + elem[i].value + "]");
qua.value = 0; // 將數量設為 0
subtotal.value = 0;
}
}
else {
var qua = document.getElementById("qua[" + elem.value + "]");
var subtotal = document.getElementById("sub[" + elem.value + "]");
qua.value = 0; // 將數量設為 0
subtotal.value = 0;
}
var total_value = document.getElementById("total");
total_value.value = 0;
<?php $chaObj->printScript(); // 輸出呼叫回應函式的程式碼 ?>
}
</script>
<?php $xajax->printJavaScript('/'); ?>
<!-- ------------------- 購物車表單及表頭 ------------------- -->
<link rel="StyleSheet" type="text/css" href="../module.css" />
<form name="form1" id="form1" method="post" action="checkout.php">
<table width="800" border="0" align="center"
cellspacing="0" cellpadding="2"
style="text-align:center;border:1px solid silver">
<tr><th colspan="7">cart</th></tr>
<tr style="background-color:silver;color:white">
<td height="23" width="80">all
<!----- 呼叫函式選取或取消全部的 checkbox ----->
<input type="checkbox" name="all"
onClick="select_all('form1','p_id[]',this.name);">
</td>
<td height="23" width="460">product name</td>
<td height="23" width="60">unit price</td>
<td width="10" height="23"></td>
<td height="23" width="60">quantity</td>
<td width="10" height="23"></td>
<td width="70" height="23">subtotal</td>
</tr>
<!-- -------------------- 輸出購物車內容 -------------------- -->
<?php
// 呈現購物車表格內容
$total = 0;
foreach($_SESSION['cart'] as $p_id => $item) {
// 將此列的 id 屬性設為 $p_id, 以方便移除產品
echo "<tr id='$p_id'>\n";
echo "<td width='80' height='21'>" .
"<input type='checkbox' name='p_id[]' value='" .
$p_id . "'></td>\n";
echo "<td>" . $item['name'] . "</td>\n";
echo "<td width='60' >" . $item['price'] . "</td>\n";
echo "<td width='10' style='border-width:0'>×</td>\n";
echo "<td width='60'>" .
"<input type='text' name='qua[$p_id]' id='qua[$p_id]'
value='" . $item['quantity'] . "' size='3'></td>";
echo "<td width='10' style='border-width:0'>=</td>\n";
echo "<td width='70' align='right'>" ."<input type='text' size='6' id='sub[$p_id]' value='".($item['quantity'] * $item['price'])."' disabled>"."</td></tr>\n";
}
?>
<tr>
<td align="right" colspan="6">total:</td>
<td align="right" style="border-top:1px solid silver;width:70">
<input type="text" name="total" value="<?php echo gettotal(); // display total amount ?>" id="total">
</td>
</tr>
</table>
<!-- -------------------- cart button -------------------- -->
<table width="800" border="0" align="center">
<tr>
<td height="23" align="center">
<input type="button" name="DEL" value="remove products"
onClick="settozero('form1','p_id[]')">
</td>
<td height="23" align="center">
<input type="button" name="UPD" value="change quantity"
onClick="xajax_change_quantity(xajax.getFormValues('form1'))">
</td>
<td height="23" align="right">
<input type="button" name="CONT" value="continue shopping"
onClick="window.close();">
</td>
<td height="23" align="right">
<input type="button" name="CONT" value="checkout"
onClick="location.href='checkout.php';">
</td>
</tr>
</table>
</form>
isn't "innerHTML"... because tag id="total" is element form input,
correct is "value"
$objResponse->assign('total','innerHTML',gettotal());
correct...
$objResponse->assign('total','value',gettotal());
I have a database table and that table has 6 rows. What I want is to display that 6 rows in a html page using a 3 column and 2 row table.
I know how to work with php arrays and while loops. My problem is how to limit the array to put 3 items in the first row and put the other 3 in the next row.
this is what i have tried but didn't work
<div id="maincontent">
<!-- class one -->
<?php
$getSection = getSection();
$i=0;
while($allSection = mysql_fetch_array($getSection)){
?>
<div class="subconent">
<table width="937" border="0">
<tr>
<td>
<div class="sub_image">
<img src="admin/uploads/fron_sect/<?php echo $allSection['image']; ?>" width="134" height="120" border="0" alt="HNA" class="PopBoxImageLink" onmouseover="PopEx(this,-50,-25,205,186,20,null);" onclick="window.location='http://localhost/hants/section.php?id=<?php echo urlencode($allSection['id']); ?>'" />
</div>
<div class="cont_txt">
<h3><?php echo $allSection['name_full']; ?></h3>
<p><?php echo substr($allSection['description'],0,140) . ""; ?></p>
<br />
<img src="images/read_more.jpg" alt="Read More" width="89" height="25" border="0" />
</div>
</td>
</tr>
</table>
</div>
<?php
if($i==4) { ?>
<table width="937" border="0">
<tr>
<td> </td>
<td> </td>
<td> </td></tr>
<tr><div class="sub_image">
<img src="admin/uploads/fron_sect/<?php echo $allSection['image']; ?>" width="134" height="120" border="0" alt="HNA" class="PopBoxImageLink" onmouseover="PopEx(this,-50,-25,205,186,20,null);" onclick="window.location='http://localhost/hants/section.php?id=<?php echo urlencode($allSection['id']); ?>'" />
</div>
<div class="cont_txt">
<h3><?php echo $allSection['name_full']; ?></h3>
<p><?php echo substr($allSection['description'],0,140) . ""; ?></p>
<br />
<img src="images/read_more.jpg" alt="Read More" width="89" height="25" border="0" />
</div><td>
<?php }
} ?>
</div>
Use modulo operator (%):
http://www.devchunks.com/web-development/using-the-php-modulus-operator/
something like this:
<table>
<?php
$i = 0;
while ( $row = mysql_fetch_array($result) ){
if ($i % 3 == 0){
echo '<tr>';
}
echo '<td>'.$row['column_name'].'</td>';
if ($i % 3 == 2){
echo '</tr>';
}
$i++;
}
//here is a check in case you don't have multiple of 3 rows
if ($i % 3 != 0){
echo '</tr>';
}
?>
</table>
At its base, you'll need something like this:
<table>
<tr>
<?
$count = 0;
foreach ($row) {
echo "<td>" . $row["value"] ."</td>";
$count++;
if (($count % 3) == 0) && ($count > 0) {
echo ("</tr><tr>");
}
}
?>
</tr>
</table>
Start printing out the header of your table, and then begin iterating through the dataset. Keep track of how many you've printed out, and if this is the third one, print the HTML to finish this row and start the next one. (I've used %, so it'll wrap on every third entry, not just the first one)
Well, you could correctly fetch those informations in your sql-query ( just one example that could fit http://en.wikibooks.org/wiki/MySQL/Pivot_table ).
Or simply fetch everything into PHP arrays.
Oldschool: mysql_query() and while( $row = mysql_fetch_array() )
Newchool: PDO ( http://de.php.net/manual/en/book.pdo.php )
Awesome! Thanks a lot. It works for me, Zend. You can try something like this.
<table width="1024px" border="0" cellspacing="2" cellpadding="2">
<?php
$i = 0;
foreach ($this->rows as $row )
{
$img = IMAGE_PATH . '/' . 'gallery/' . $row->gly_thumbnail;
if ($i % 3 == 0)
{
echo '<tr>';
}
?>
<td align="center">
<img src="<?php echo $img; ?>" width="300" height="215"><br/>
<?php echo $row->gly_title; ?>
</td>
<?php
if ($i % 3 == 2)
{
echo '</tr>';
}
$i++;
}
//here is a check in case you don't have multiple of 3 rows
if ($i % 3 != 0)
{
echo '</tr>';
}
?>
</table>
<?php if ($i++%$_columnCount==0): ?>
<tr>
<?php endif ?>
<td> <img src="<?php echo site_url('uploads/shelter_images/'.$row->shelter_id."/".$img->imagefile) ?>" alt="" width="300" ></td>
<?php if ($i%$_columnCount==0 || $i==$totalImg): ?>
</tr>
<?php endif; ?>
<?php } ?>
I am populating the images from my database in a table, how to restrict the images to three per row?
<table border="0" align="center" height="25%" width="25%" >
<tr><td align="center" width="50px" bgcolor="#4b2d0e"><strong><font color="#FFFFFF">Friend List</font></strong></td></tr>
<? foreach($Selected as $row)
{
$value = $row['dPath'];
$imgp = base_url()."images"."/".$value;
?>
<tr><td bgcolor="#999999">
<strong ><?=$row['dFrindName'].'</br>';?></strong>
<? if($value!="") { ?>
<a class="Tab_Link" href="<? echo site_url();?>/friends/View_FProfile/<?=$row['dMember_Id']?>"><img src="<?=$imgp ?>" name="b1" width="90" height="80" border="0"/></a><br>
<? } else { ?>
<a class="Tab_Link" href="<? echo site_url();?>/friends/View_FProfile/<?=$row['dMember_Id']?>"><img src="../images/us.png" width="90px" height="80px"></a>
<? }?>
</td></tr>
<? }}?>
</table>
This is my table
<?php
$x=0;
foreach($Selected as $row){
if($x%3 == 0)
echo '<tr>';
$value = $row['dPath'];
$imgp = base_url()."images"."/".$value;
?>
<td style="background-color:#999999;">
<strong ><?=$row['dFrindName'].'</br>';?></strong>
<?php if($value!="") {?>
<a class="Tab_Link" href="<? echo site_url();?>/friends/View_FProfile/<?=$row['dMember_Id']?>"><img src="<?=$imgp ?>" name="b1" width="90" height="80" border="0"/></a><br>
<?php } else { ?>
<a class="Tab_Link" href="<? echo site_url();?>/friends/View_FProfile/<?=$row['dMember_Id']?>"><img src="../images/us.png" width="90px" height="80px"></a>
<?php }?>
</td>
<?php
if($x%3 == 0)
echo '</tr>';
x++;
}
if(($x-1)%3 != 0)
echo '</tr>'; //Prints out the last '<tr>' if one isn't printed.
?>
You need to use an if with a modulus operator.
Here, I cleaned up your invalid HTML, used CSS, and used a more recommended PHP coding style.
Please note: you need to be aware that if $Selected contains user-inputted (or otherwise non-HTML-safe) data, you need to wrap your output in htmlspecialchars or be open to XSS vulnerabilities.
It was a little unclear what you meant by wanting to "restrict the images to three per row" considering that it was only showing 1 per row currently. If I am to assume that you want to show 3 per row rather than 1, than you need to use the modulus operator and only open a new <tr> after every third element, and then close it at the right time. Like this:
<style type="text/css">
a img { border: none; }
.friend-list { border: none; width: 25%; height: 25%; margin: 0 auto; }
.friend-list th { text-align: center; background-color: #4b2d0e; color: #fff; font-weight: bold; }
.friend-list td { background-color: #999999; }
</style>
<?php
$numCols = 3;
$colCount = -1;
?>
<table class="friend-list">
<tr>
<th colspan="<?php echo $numCols; ?>">Friend List</th>
</tr>
<?php
foreach($Selected as $row) {
$value = $row['dPath'];
$imgp = ($value) ? base_url().'images/'.$value : '../images/us.png';
if(++$colCount % $numCols == 0) {
echo '<tr>';
}
?>
<td>
<strong><?php echo $row['dFriendName']; ?></strong><br />
<a class="Tab_Link" href="<?php echo site_url(); ?>/friends/View_FProfile/<?php echo $row['dMember_Id']; ?>">
<img src="<?php echo $imgp; ?>" width="90" height="80" />
</a>
</td>
<?php
if(($colCount + 1) % $numCols == 0) {
echo '</tr>';
} elseif (($colCount + 1) == count($Selected)) {
// if 16 elements are to fit in 3 columns, print 2 empty <td>s before closing <tr>
$extraTDs = $numCols - (($colCount + 1) % $numCols);
for ($i = 0; $i < $extraTDs; $i++) {
echo '<td> </td>';
}
echo '</tr>';
}
}
?>
</table>
Tables should be reserved for situations where columns and rows have meaning... A better solution is to use floated block elements instead of table cells. When you float a bunch of similar blocks, they wrap automatically, so the key is making their parent container just wide enough to hold 3 of them.
You don't need to do anything special with php to create new rows, so i'll just display the html and css, letting you write the php to make it happen.
html:
<div id="replacesTable">
<div class="replacesTableCell">
<div class="name">Name</div>
<img src="http://stackoverflow.com/favicon.ico" />
</div>
<div class="replacesTableCell">
<div class="name">Name</div>
<img src="http://stackoverflow.com/favicon.ico" />
</div>
<div class="replacesTableCell">
<div class="name">Name</div>
<img src="http://stackoverflow.com/favicon.ico" />
</div>
<div class="replacesTableCell">
<div class="name">Name</div>
<img src="http://stackoverflow.com/favicon.ico" />
</div>
<div class="clear"> </div>
</div>
css:
#replacesTable{
width: 300px;
}
div.replacesTableCell{
float:left;
width: 100px;
/* styles below are just to make it easier to see what's happening */
text-align:center;
font-size: 10px;
margin: 20px 0;
}
/* this just stretches the parent container #replacesTable around the entries*/
.clear{
clear:both;
height:1px;
overflow:hidden;
}
You can use CSS as an alernative if the images are of a fixed width and you can do without the tables - create a wrapper div with a fixed width around your entire image list, and simply float each image left.
Here is a simplified example without the extraneous styling information to show the principal. Every 3rd image we want to write the opening and closing tags (though not at the same time).
So we loop through the list of images and use the moduulus operator to know when we should print the <tr> tags.
<?php
$column_count = 3;
$image_list = get_images();
?>
<table>
<?php
for($i=0; $i < sizeof($image_list); i++) {
$cur_img = $image_list[$i];
$img_url = $cur_img['url'];
// open a we row every 3rd column
if($i % $column_count == 0) {
print '<tr>';
}
// for every image we want a table cell, and an image tag
print "<td> <img src='$img_url'> </td>";
// close the row every 3rd column, but offset by 3 from the opening tag
if($i % $column_count == $column_count - 1) {
print '<tr>';
}
}
// what if the number of images are not div by 3? Then there will be less
// than 3 items in the last row, and no closing tag. So we look for that and
// print a closing tag here if needed
if(sizeof($image_list) % $column_count != 0) {
print '</tr>';
}
?>
</table>
You can try using http://www.php.net/manual/en/function.array-chunk.php
Hey, would you try this.
Notice that I replaced the if...else with a ternary operator. I prefer it compact.
Hope it helps you and anyone else interested.:)
<table border="0" align="center" height="25%" width="25%" >
<tr>
<td colspan="3" align="center" width="50px" bgcolor="#4b2d0e">
<strong><font color="#FFFFFF">Friend List</font></strong>
</td>
</tr>
<?
$imgs=0;
foreach($Selected as $row){
$value = $row['dPath'];
$imgp = base_url()."images"."/".$value;
if($imgs%3 == 0){
echo '<tr>';
}
?>
<td bgcolor="#999999">
<strong ><?=$row['dFrindName'].'</br>';?></strong><a class="Tab_Link" href="<? echo site_url();?>/friends/View_FProfile/<?=$row['dMember_Id']?>"><img src="<?=$value!=""? $imgp: '../images/us.png' ?>" name="b1" width="90" height="80" border="0"/></a>
</td>
<?
$imgs++;
if($imgs%3 == 0){
echo "</tr>\n";
}
}//end loop
echo '</tr>';//end last row
?>
</table>
I want to show nine images in a table , three images in each row using PHP.
It mostly depends on how you have PHP representing the images. The HTML markup should look something like
<div class="img-table">
<img ...>
<img ...>
<img ...>
....
</div>
and the CSS:
.img-table {
width:1000px; /* or whatever works */
}
.img-table img {
width:30%;
height:auto;
display:inline;
margin: 10px 1.5%; /* spacing: 20px vertically, 5% horizontally */
}
or, if you want to use <table> tables:
<table class="img-table">
<tr>
<td><img ...></td>
<td><img ...></td>
<td><img ...></td>
</tr>
.....
</table>
If you have your image urls located in a array, like
$imgs = array('path/to/img1.jpg','path/to/img2.jpg',...);
then you can generate the HTML like this:
$index = 0;
$html = '<div class="img-table">';
for ($i=0; $i<3; $i++) {
for ($j=0; $j<3; $j++) {
$html .= '<img src="' . $imgs[$index++] . '">';
}
}
$html .= '</div>';
echo $html;
or for the table:
$index = 0;
$html = '<table class="img-table">';
for ($i=0; $i<3; $i++) {
$html .= '<tr>'
for ($j=0; $j<3; $j++) {
$html .= '<td>';
$html .= '<img src="' . $imgs[$index++] . '">';
$html .= '</td>';
}
}
$html .= '</table>';
echo $html;
Hope that helps.
Laying out column images in a table
<? $perrow=3; $n=0; ?>
<table>
<? foreach($images as $i): ?>
<? if($n == 0): print '<tr>'; endif; ?>
<td><img src="<?=$i?>"></td>
<? if(++$n >= $perrow): print '</tr>'; $n = 0; endif; ?>
<? endforeach; ?>
</table>
<?php
$images=ARRAY( a.gif","b.jpeg","c.jif","d.bmp");
?>
<table border="0" cellspacing="1" cellpadding="0">
<tr>
<?php
for($i=0; $i< count($images); $i++) {
echo "<td>
<table width=100% border=0>
<tr><td>
<img src=Download/$images[$i] width=130 height=130 border=0></td></tr>
</table>
</td>";
if ( (($i+1) % 3) == 0 ) echo $newrow="</tr><tr>"; // change 6 to 8 and see
}
?>
</tr>
</table>
Sorry for asking an implement my feature question type question last time. I am new to Stackoverflow.com and also to php that's why.
What I was trying to ask is:
I have made a admin account. Members have registration page so a member will register. When user registers in the database table I will have a field for which 0 value will be initialised which means he is not approved. In admin account I have code to get the list of members. The code is given below:
<h2><?php echo "User list"; ?></h2>
<table border="0" cellpadding="0" cellspacing="0">
<tr bgcolor="#f87820">
<td><img src="img/blank.gif" alt="" width="10" height="25"></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="150" height="6"><br><b><?php echo "first name"; ?></b></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="150" height="6"><br><b><?php echo "lastname name"; ?></b></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="150" height="6"><br><b><?php echo "member id"; ?></b></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="50" height="6"><br><b><?php echo "delete"; ?></b></td>
<td><img src="img/blank.gif" alt="" width="10" height="25"></td>
</tr>
<?php
}
$result=mysql_query("SELECT member_id,firstname,lastname,login FROM members ORDER BY firstname");
$i = 0;
while($row = mysql_fetch_array($result)) {
if ($i > 0) {
echo "<tr valign='bottom'>";
echo "<td bgcolor='#ffffff' height='1' style='background-image:url(img/strichel.gif)' colspan='6'></td>";
echo "</tr>";
}
echo "<tr valign='middle'>";
echo "<td class='tabval'><img src='img/blank.gif' alt='' width='10' height='20'></td>";
echo "<td class='tabval'><b>".$row['lastname']."</b></td>";
echo "<td class='tabval'>".$row['firstname']." </td>";
echo "<td class='tabval'>".$row['member_id']." </td>";
echo "<td class='tabval'><a onclick=\"return </span></a></td>";
echo "<td class='tabval'></td>";
echo "</tr>";
$i++;
}
?>
</table>
in this i wanna add tho more things in the table 1 to delete a member and 2 to have approved or denied option for that i made two functiom
below code is to delete
if($_REQUEST['action']=="del")
{
$memberId = mysql_real_Escape_string($_REQUEST['member_id']);
mysql_query("DELETE FROM members WHERE member_id=$memberId");
}
below one for approving members
But my problem is I don't know how to include a button or radio button in the table which can pass value delete or approve to these functions.
Please tell me how the syntax is to add this button so that for approving I can change the value 0 that I gave in the database to 1 so that member get approved.
Try this:
echo '<td><a href="http://yourwebsite/yourscriptname.php?action=del&member_id='
. htmlspecialchars($row['member_id']) . '">Delete</a>';
if ($row['approved'] == 0) {
echo ' <a href="http://yourwebsite/yourscriptname.php?action=approve&member_id='
. htmlspecialchars($row['member_id']) . '">Approve</a>';
}
echo '</td>';
And make sure ALL of your database values are being sent to the browser in htmlspecialchars().
On the flipside,
$member_id = 0;
if (isset($_GET['member_id'])) $member_id = intval($_GET['member_id']);
$action = '';
if (isset($_GET['action'])) $action = $_GET['action'];
$sql = '';
switch($action) {
case 'approve':
$sql = "UPDATE members SET approval = 1 WHERE member_id = $member_id";
break;
case 'delete':
$sql = "DELETE FROM member WHERE member_id = $member_id";
break;
}
if (!empty($sql) && !empty($member_id)) {
// execute the sql.
}
What I would do is to set up a form inside of the table.
?> <form name="deleteUser" id="deleteUser" method="post" action="">
<input type="hidden" name="member_id" id="member_id" value="<?php echo $row['member_id'] ?>
<input type="submit" name="action" id="action" value="del" />
</form><?php
I would insert that in between your <td> tag.
<td class='tabval'>INSERT HERE</td>";