Put MySQL request result in table - php

<?PHP
?>
<html>
<head>
<title></title>
<link rel="stylesheet" href="one.css" type="text/css">
</head>
<?
include_once "db.php";
$result = mysql_query("SELECT * FROM products WHERE product_type='weddingdressaline' ORDER BY user_id");
{
echo "<table width='100%' border='5' bordercolor='#860071' cellspacing='5' cellpadding='5'>";
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
$i = 0;
while ($i < 3)
{
echo '<td><table width="100%" border="0" cellspacing="0" cellpadding="0" BGCOLOR="#AA99C5"><TR><TD><div class="container"><img src="showimage.php?user_id=' . $row["user_id"] . '" ALIGN="CENTER" /></div></td><tr><td><CENTER><STRONG>' . $row['price'] . '</STRONG></td></TABLE>';
$i++;
}
echo "</tr>";
}
} else {
echo "<tr><td colspan='" . ($i + 1) . "'>No Results found!</td></tr>";
}
echo "</table>";
}
?>
The code is suppose to put one item into one column. Unfortunately its placing one result for the entire row. If anyone has any ideas how to fix this please help.

You have some access brackets in your code:
$result = mysql_query("SELECT * FROM products WHERE product_type='weddingdressaline' ORDER BY user_id");
{ <===
echo "<table width='100%' border='5' bordercolor='#860071' cellspacing='5' cellpadding='5'>";
[....]
echo "</table>";
} <===

First of all please learn some clean coding standards for everyones sake:
http://drupal.org/coding-standards
The second problem is that your are outputting this in the wrong. Trying to use tables for one (use divs) is just poor markup these days. All your styles should be in your CSS, not embedded on the page.
Here is your greatly improved markup. You use CSS to change how it is displayed on the page, not a table structure.
<html>
<head>
<title></title>
<link rel="stylesheet" href="one.css" type="text/css">
</head>
<body>
<?php
include_once "db.php";
$result = mysql_query("SELECT * FROM products WHERE product_type = 'weddingdressaline' ORDER BY user_id");
echo '<div class="productTable">';
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_object($result)) {
echo '
<div class="container">
<div class="productImage">
<a href="viewproduct.php?user_id=' . $row->user_id . '">
<img src="showimage.php?user_id=' . $row->user_id . '" align="center" />
</a>
</div>
<div class="price">' . $row->price . '</div>
</div>';
}
}
else {
echo '<div class="container"><div class="noResults">No Results found!</div></div>';
}
echo "</div>";
?>
</body>
</html>

Related

PHP How to use for loop's variable with mysql_fetch_array to indicate row to get data?

I have trouble to use for loop's variable $counter with
$HA_Row = mysql_fetch_array($HA_Res);
to indicate which row's data I am going to get. Basically, I have no idea to use $counter and $HA_Res together.
I want to it can display different rows' data of my database.
For example, like
echo "$HA_Row[Check_In_Date]"
I have tried
echo "$HA_Row[$counter][Check_In_Date]"
and
echo "$HA_Row[Check_In_Date][$counter]", echo "[$counter]$HA_Row[Check_In_Date]"
but none of them works.
Thank you so much! Appreciate your time.
<?php
session_start();
include_once 'dbconnect.php';
if(!isset($_SESSION['Hotel_User_ID']))
{
header("Location: index.php");
}
$res = mysql_query("SELECT * FROM Hotel_Info WHERE Hotel_ID =".$_SESSION['Hotel_User_ID']);
$userRow = mysql_fetch_array($res);
$HA_Res = mysql_query("SELECT * FROM Hotel_Account WHERE Hotel_ID =".$_SESSION['Hotel_User_ID']);
$HA_Row = mysql_fetch_array($HA_Res);
$HA_Count = mysql_num_rows($HA_Res);
// echo "$HA_Count". "<br>";
// echo "$HA_Row[Check_In_Date]";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hello - <?php echo $userRow['Hotel_Email']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<div id="left">
<label>Welcome <?php echo $userRow['Hotel_Uname']; ?> </label>
</div>
<div id="right">
<div id="content">
Sign Out
</div>
</div>
</div>
<div id="menu">
Manage <br>
Update
</div>
<div id="body">
<center>
<div id="Hotel-InfoForm">
<form method="post" enctype="multipart/form-data">
<!-- <table.ver2 align="center" width="60%" border="0"> -->
<table align="center" width="85%" border = "1">
<tr>
<th>Check In Data</th>
<th>Check Out Data</th>
<th>Theme</th>
<th>Bed Size</th>
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>Age</th>
<th>Phone Number</th>
<th>Email</th>
</tr>
<?php
for($counter = 0; $counter < $HA_Count; $counter = $counter + 1)
{
// using $counter for $HA_Row[....]
echo "<tr>";
echo "<td>";
echo "$HA_Row[Check_In_Date]";
echo "</td>";
echo "<td>";
echo "$HA_Row[Check_Out_Date]";
echo "</td>";
$Theme_Res = mysql_query("SELECT * FROM Theme WHERE Theme_ID = ".$HA_Row[Theme_ID]);
$Theme_Row = mysql_fetch_array($Theme_Res);
echo "<td>";
echo "$Theme_Row[Theme_Name]";
echo "</td>";
echo "<td>";
echo "$Theme_Row[Bed_Size]";
echo "</td>";
$Guest_Res = mysql_query("SELECT * FROM Guest_Info WHERE Guest_ID = ".$HA_Row[Guest_ID]);
$Guest_Row = mysql_fetch_array($Guest_Res);
echo "<td>";
echo "$Guest_Row[First_Name]";
echo "</td>";
echo "<td>";
echo "$Guest_Row[Last_Name]";
echo "</td>";
echo "<td>";
echo "$Guest_Row[Gender]";
echo "</td>";
function ageCalculator($dob)
{
if(!empty($dob))
{
$birthdate = new DateTime($dob);
$today = new DateTime('today');
$age = $birthdate->diff($today)->y;
return $age;
}
else
{
return 0;
}
}
echo "<td>";
echo ageCalculator($Guest_Row[Birth_Day]);
echo "</td>";
echo "<td>";
echo "$Guest_Row[Phone_Num]";
echo "</td>";
echo "<td>";
echo "$Guest_Row[Email]";
echo "</td>";
echo "</tr>\n";
}
?>
</table>
</form>
</div>
</center>
</div>
</body>
</html>
You could use mysql_fetch_assoc and while looping, like this:
while($HA_Row = mysql_fetch_assoc($HA_Res)){
echo $HA_Row[Check_In_Date]."<br>";
};
This while will loop in each instance!
You don't normally use a counter to control fetch, especially if you just want to process the records in sequence. Each call to fetch will return the next row in the result set without further action on your part.
First, replace all your mysql_* calls with their mysqli_* counterparts. Then replace your line
for($counter = 0; $counter < $HA_Count; $counter = $counter + 1)
with
while ($HA_Row = mysqli_fetch_array($HA_Res))
On each loop, mysqli_fetch_array returns the next row, and after reading the last row the next call returns false, which exits the loop.
Switching to mysqli_* or PDO will also gain you prepared statements, which help to protect against SQL injection, among numerous other benefits.

echo two tables issues required to post twice (mysql, php)

I finally get reviews to work, which will allow user to post their review about the product and display them in a review page with product details, however I can't get it to work perfectly. When a user posts their review, it will update in the table but only the second post will show up.
The following image is from the test i was running,
Before posting a review
After posting the first review
After posting the second review
As the images display, the first review will never show up, only starting from the second and above,
Here my code for review page updated with full code
<?php
if (!isset($_SESSION)) {session_start();} //start session
if (!isset($_SESSION['client_ID'])) {
//echo "<script>alert('not logged in');</script>";
header("Location: index.html" );
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="keywords" content="Games, Gaming, PS4, PS3, XBOX, Video games">
<meta name="description" content="Games 4 You">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Games 4 You</title>
<link rel="stylesheet" type="text/css" href="Styles/ProductsStyle.css">
<!-- javascript/jQuery -->
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<!--Add the following script at the bottom of the web page (before </body></html>)
Support system using MyLiveChat.com
-->
<script type="text/javascript" async="async" defer="defer" data-cfasync="false" src="https://mylivechat.com/chatinline.aspx?hccid=42206151"></script>
<script>// disable zoom to keep image fit and always in position
document.firstElementChild.style.zoom = "reset";
// the above script will disable zoom in and out
</script>
<script type="text/javascript">
// this will auto change the background image to the following 7 images which are in the root Images/
// this is set to change every five second
// declare list of backgrounds
var images = ['bg-01.jpg', 'bg-02.jpg', 'bg-03.jpg', 'bg-04.jpg', 'bg-05.jpg', 'bg-06.jpg', 'bg-07.jpg'];
// declare function that changes the background
function setRandomBackground() {
// choose random background
var randomBackground = images[Math.floor(Math.random() * images.length)];
// set background with jQuery
$('body').css('background-image', 'url("Images/' + randomBackground + '")');
}
// declare function that sets the initial background, and starts the loop.
function startLoop() {
// Set initial background.
setRandomBackground();
// Tell browser to execute the setRandomBackground every 5 seconds.
setInterval(setRandomBackground, 5 * 1000);
}
// One the page has finished loading, execute the startLoop function
$(document).ready(startLoop);
</script>
<header id="header">
<div class="container">
<center><img src="Images/Title.png" alt="Title"></div>
</center>
</header>
<center>
<nav>
<?php
echo "<p> Welcome ".$_SESSION['client_name']."</p>";
//create connection
$con = new mysqli("localhost", "student", "student", "cib4003_h00233671_at");
if ($con->connect_errno) { //failed
echo "Failed to connect to MySQL: (" . $con->connect_errno . ") " . $con->connect_error;
}?>
<div class="wrapper">
<ul id="category" >
<li>Home</li>
<li>Products</li>
<li>View Cart</li>
<li>About</li>
<li>Settings</li>
<li>Logoff</li>
</ul>
</nav>
</div>
</center>
<main>
<h3>Available Products</h3>
<?php
$product = $_GET["RID"];
$_SESSION["product_name_RID"] = $_GET["RID"];
$sql="SELECT * FROM products,reviews WHERE products.Product_Name = '$product' AND reviews.Product_Name = '$product'";
//$sql="SELECT * FROM reviews WHERE Product_Name = '$product'";
// $sql="SELECT * FROM pizza,pizzacart WHERE pizza.Pizza_ID=pizzacart.Pizza_ID AND pizzacart.client_ID=".$_SESSION['client_ID'];
//echo "connected to DB";
//run SQL query
$result = mysqli_query($con,$sql);
//output result
if(mysqli_num_rows($result)==0) //no records found
{
$sql="SELECT * FROM products WHERE Product_Name = '$product'";
$result = mysqli_query($con,$sql);
// echo "<p>no records in DB".mysqli_num_rows($result)."</p>";
// echo "<p><a href=products.php></a>";
// link has been disable because i am using the <a for something else so i can't force the image to be in the center when using <a
// so the result will only be image that tell the customers no products found click all or search with different data
?>
<table class="table-style-one">
<tr>
<th>Product Image</th>
<th>Product Name</th>
<th>Description</th>
<th>Product Type</th>
<th>Console Type</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) { //loops through records
echo "<tr>";
echo "<td><img src='".$row['picture']."'/>";
echo "<td>".$row['Product_Name']."</td>";
echo "<td>".$row['Description']." <center><b><br>".$row['Trailer']."<br></b></center></td>";
echo "<td>".$row['Product_Type']."</td>";
echo "<td>".$row['Console_Type']."</td>";
echo "</tr>";
}
//end of loop
echo "</table>";
echo "<p>No Reviews available for this product.<br> To post a review of this product, fill up the below form.</p>";
//end of else
}
else
{
?>
<table class="table-style-one">
<tr>
<th>Product Image</th>
<th>Product Name</th>
<th>Description</th>
<th>Product Type</th>
<th>Console Type</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) { //loops through records
echo "<tr>";
echo "<td><img src='".$row['picture']."'/>";
echo "<td>".$row['Product_Name']."</td>";
echo "<td>".$row['Description']." <center><b><br>".$row['Trailer']."<br></b></center></td>";
echo "<td>".$row['Product_Type']."</td>";
echo "<td>".$row['Console_Type']."</td>";
echo "</tr>";
echo "<br>";
?>
<?php
while($row = mysqli_fetch_array($result)) {
echo "<table class=table-style-one align=center>";
// echo "<tr><th>Review ID</th><td>".$row['Review_ID']."</td></tr>";
echo "<tr><th>Review By:</th><td>".$_SESSION['client_name']."</td></tr>";
echo "<tr><th>Review Title</th><td>".$row['Review_Title']."</td></tr>";
echo "<tr><th>Rate:</th><td>".$row['Review_Rate']."/5</td></tr>";
echo "<tr><th>Review</th><td colspan=2>".$row['Review']."</td></tr>";
echo "<tr><th>Submitted On</th><td>".$row['Review_Date']."</td></tr>";
echo "<br>";
?>
<?php
}
//end of loop
echo "</table>";
//end of else
}
}
?>
<table class="table-style-one" align="center">
<tr>
<form method="POST" action="submitreview.php">
<!--<th>Product Name:</th><td> <input type="text" size="30" id="ReviewTitle" name="ReviewTitle" pattern=".{5,}" required title="5 characters minimum" placeholder="Review Title"></td> -->
<th>Review Title:</th><td> <input type="text" required size="30" id="ReviewTitle" name="ReviewTitle" pattern=".{5,}" required title="5 characters minimum" placeholder="Review Title"> </td>
<th>Rate: </th>
<td>
<select name="Review_Rate" required>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</td>
<tr>
<td colspan="4">
<textarea name="WriteReview" id="WriteReview" required rows="10" cols="50" wrap="physical" placeholder="Write your Review here" style="margin: 0px; width: 437px; height: 150px;"></textarea>
</td>
</tr>
<td align="center" colspan="2"><input type="submit" value="submit"></td>
<td colspan="2"><input type="Reset"></td>
</form> </tr>
</table>
</h2>
<br>
<br>
<br>
</main>
</body>
<footer>
<p>Made by Humaid Al Ali - H00233671</p>
<div id="google_translate_element"></div><script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL, multilanguagePage: true}, 'google_translate_element');
}
</script><script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
</footer>
</html>
and here the php file where I insert the review into the table
<?php
if (!isset($_SESSION)) {session_start();} //start session
if (!isset($_SESSION['client_ID'])) {
//echo "<script>alert('not logged in');</script>";
header("Location: index.html" );
}
?>
<?php
//new connection
$con = new mysqli("localhost", "student", "student", "cib4003_h00233671_at");
if ($con->connect_errno) { //failed
echo "Failed to connect to MySQL: (" . $con->connect_errno . ") " . $con->connect_error;
}
//success
//if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// run sql
$sql ="INSERT INTO `cib4003_h00233671_at`.`reviews`(`Review_ID`, `Product_Name`, `client_ID`, `Review_Title`, `Review_Rate`, `Review`) VALUES (NULL, '".$_SESSION['product_name_RID']."', '".$_SESSION['client_ID']."', '".$_POST["ReviewTitle"]."', '".$_POST['Review_Rate']."', '".$_POST['WriteReview']."');";
if ($con->query($sql) === TRUE) {echo "<h3> New record created successfully</h3>";
header('Location: '. $_SERVER['HTTP_REFERER'] );
} else {
echo "Error : " . $sql . "<br>" . $con->error;
}
$con->close();
?>
Problem:
As the images display, the first review will never show up, only starting from the second and above
That's because you're fetching the first row which includes the first review but you don't display it, rather you starting fetching next reviews and display them. Look at the two conditions in while block inside the else block.
Solution:
To display reviews, you should do something like this:
<?php
// your code
if(mysqli_num_rows($result)==0){
// your code
}else{
echo "<table class='table-style-one'>";
echo "<tr>";
echo "<th>Product Image</th>";
echo "<th>Product Name</th>";
echo "<th>Description</th>";
echo "<th>Product Type</th>";
echo "<th>Console Type</th>";
echo "</tr>";
// fetch first row, which also contains first review
$row = mysqli_fetch_array($result);
echo "<tr>";
echo "<td><img src='" . $row['picture'] . "'/>";
echo "<td>" . $row['Product_Name'] . "</td>";
echo "<td>" . $row['Description'] . "<center><b><br>" . $row['Trailer'] . "<br></b></center></td>";
echo "<td>" . $row['Product_Type'] . "</td>";
echo "<td>" . $row['Console_Type'] . "</td>";
echo "</tr>";
echo "</table>";
echo "<table class=table-style-one align=center>";
echo "<tr>";
echo "<th>Review ID</th>";
echo "<th>Review By:</th>";
echo "<th>Review Title</th>";
echo "<th>Rate:</th>";
echo "<th>Review</th>";
echo "<th>Submitted On</th>";
echo "</tr>";
do{
// display first review and then fetch rest of the reviews
echo "<tr>";
echo "<td>" . $row['Review_ID'] . "</td>";
echo "<td>" . $_SESSION['client_name'] . "</td>";
echo "<td>" . $row['Review_Title'] . "</td>";
echo "<td>" . $row['Review_Rate'] . "/5</td>";
echo "<td colspan=2>" . $row['Review'] . "</td>";
echo "<td>" . $row['Review_Date'] . "</td>";
echo "</tr>";
}while($row = mysqli_fetch_array($result));
echo "</table>";
?>
</table>
<?php
}
// your code
?>
The problem looks to me to be in your SQL query. It looks as though your SQL query should actually be doing a join on the related column to bring together data across two different tables
$sql="SELECT *
FROM products AS p,
JOIN reviews AS r
ON p.Product_Name = r.Product_Name
WHERE products.Product_Name = '$product'";
But I think the easiest solution would be to run another query within the table output loop where you get the reviews for the product name you are currently outputting and loop over those.

add/remove data from table from previous page

I have an HTML table being displayed from php data and I'm trying to move data from one table to another based on what is clicked. I cannot get it to move from table to another but I"m not getting any code error.
<html>
<head>
<title>View Requests</title>
</head>
<body>
<div class="breadcrumbs">
<center>
Home · Requests
</center>
</div>
<?php
require_once '../../scripts/app_config.php';
require_once '../../scripts/database_connection.php';
// get results from database
$result = mysql_query("SELECT * FROM temp")
or die(mysql_error());
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['User_id'] . '</td>';
echo '<td>' . $row['First_name'] . '</td>';
echo '<td>' . $row['Last_name'] . '</td>';
echo '<td>' . $row['Username'] . '</td>';
echo '<td>Approve</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
</body>
</html>
PHP EDIT SCRIPT:
<html>
<head>
<title>View Requests</title>
</head>
<body>
<div class="breadcrumbs">
<center>
Home · Requests · All Users
</center>
<?php
require_once '../../scripts/app_config.php';
require_once '../../scripts/database_connection.php';
$id = $_GET['id'];
$sql = ("INSERT INTO users
SELECT * FROM temp WHERE User_id = $id ");
mysql_query($sql)
or die(mysql_error());
?>
</body>
</html>
I needed to use
$id = $_get['id];
and in the insert statement i had to replace id with User_id
<html>
<head>
<title>View Requests</title>
</head>
<body>
<div class="breadcrumbs">
<center>
Home · Requests · All Users
</center>
<?php
require_once '../../scripts/app_config.php';
require_once '../../scripts/database_connection.php';
$id = $_GET['id'];
$sql = ("INSERT INTO users
SELECT * FROM temp WHERE User_id = $id ");
mysql_query($sql)
or die(mysql_error());
?>
</body>
</html>

Fixing a pagination error

I am having a problem with pagination within the tabs. In the second tab (candidate), when I press pagination 2 to navigate to the second page of the candidate table, I am returned to the first page of the contact table. If I go to the candidate tab after that I am on the right page. Where have I gone wrong?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"> </script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/start/jquery-ui.css"/>
<link type ="text/css" rel ="stylesheet" href = "testData.cs"/>
<script>
$(function(){
$('#tabs1,#tabs2').tabs();
});
</script>
<head>
<title>Candidate DB</title>
</head>
<body>
<div id ="tabs1" class ="contactForm">
<ul>
<li>List of Contacts</li>
<li>List of Candidates</li>
<li>Advanced Search</li>
</ul>
<div id ="tab1" class="contact" >
<table border="1" id="contact_info">
<tr>
<th>Contact Name</th>
<th>County</th>
</tr>
<?php
$DB_NAME = "Candidate";
$DB_USER ="root";
$DB_PASSWORD ="";
$DB_HOST ="localhost";
$con=mysql_connect("$DB_HOST", "$DB_USER", "$DB_PASSWORD") or die("Could not connect to MySQL");
mysql_select_db("$DB_NAME") or die ("No Database");
echo "Connected to Candidate Database </br></hr>" ;
$per_page=5;
$pages_query= mysql_query("SELECT COUNT(contact_id) FROM contact");
$pages = ceil(mysql_result($pages_query,0)/$per_page);
$page=(isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start=($page-1) * $per_page;
$sql=mysql_query("SELECT first_name, last_name, county from contact ORDER BY last_name ASC LIMIT $start, $per_page" );
$Fname = 'first_name';
$Lname = 'last_name';
$county = 'county';
while ( $row = mysql_fetch_object($sql)) {
echo "<tr>";
echo "<td>" . $row ->first_name . " " . $row->last_name. "</td>";
echo "<td>" . $row->county . "</td>";
echo "</tr>";
}
// close the loop
if ($pages>=1 && $page<= $pages ) {
for ($x=1; $x<=$pages; $x++)
{
echo ($x ==$page)? '<strong><a style="color:green" href="? page='.$x.'">'.$x. '</a></strong>_':''.$x. '_';
}
}
?>
</table>
</div>
<div id ="tab2" class="candidate" >
<table border="1" id="candidate_info">
<tr>
<th>Candidate Name</th>
<th>County</th>
</tr>
<?php
$per_pageR=5;
$pages_queryR= mysql_query("SELECT COUNT(candidate_id) FROM p_candidate");
$pagesR = ceil(mysql_result($pages_queryR,0)/$per_pageR);
$pageR=(isset($_GET['pageR'])) ? (int)$_GET['pageR'] : 1;
$startR=($pageR-1) * $per_pageR;
$sql2=mysql_query("SELECT R_first_name, R_last_name, R_county from p_candidate ORDER BY R_last_name ASC LIMIT $startR, $per_pageR" );
$R_name = 'R_first_name';
$R_name = 'R_last_name';
$R_county = 'R_county';
while ( $rowR = mysql_fetch_object($sql2)) {
echo "<tr>";
echo "<td>" . $rowR ->R_first_name . " " . $rowR->R_last_name. "</td>";
echo "<td>" . $rowR->R_county . "</td>";
echo "</tr>";
}
// close the loop
if ($pagesR>=1 && $pageR<= $pagesR ) {
for ($y=1; $y<=$pagesR; $y++)
{
echo ($y ==$pageR)? '<strong><a style="color:green" href="? pageR='.$y.'">'.$y. '</a></strong>_':''.$y. '_';
}
}
?>
</table>
</div>
</div>
</body>
If I understand correctly, try using the active option - http://api.jqueryui.com/tabs/#option-active - when you do .tabs() to make the candidate tab the active tab.
<script>
$(function(){
$('#tabs1,#tabs2').tabs(<?php if(isset($_GET['pageR'])) echo "{ active: 1 }"; ?>);
});
</script>
or
<script>
$(function(){
$('#tabs1,#tabs2').tabs(<?php if(isset($_GET['pageR'])) echo '"option", "active", -1'; ?>);
});
</script>
edit
You could bind the .tabs() first, and then set the active option. This should hopefully fix your formatting issue.
<script>
$(function(){
$('#tabs1,#tabs2').tabs();
<?php if(isset($_GET['pageR'])) { ?>
$('#tabs1').tabs("option", "active", -1)
<?php } ?>
});
</script>

How to color even/odd table rows in PHP

OK I have a question I have this code which I will list below. I need to make the even rows a light blue and the odd a white. Now it doesnt show up so I am assume I am doing soemthing wrong. Now do I need to do the order in it so that way the rows will look the way I need it to do?
<html>
<head>
<title> Html Tables</title>
</head>
<body>
<?php
echo "<table width=\"50%\" cellpadding=\"2\" cellspacing=\"2\" border=\"1\">";
echo "<tr bgcolor=\"#FFFFFF\">";
$rowcount=0;
for($x=1;$x<=12;$x++){
echo " <td align=\"center\" style=\"width:100px\">".$x."</td>\n";
if ($x%4==0) {
if ($rowcount%2==0){
echo "</tr>";
echo "<tr bgcolor=\"#5CCDC9\">\n";
}
else{
echo "</tr>";
echo "<tr bgcolor=\"#FFFFFF\">\n";
}
$rowscount++;
}
}
echo "</tr>";
echo "</table>";
?>
</body>
</html>
ok I am trying to understand this better after reading a few things this is my new code
<html>
<head>
<title> Html Tables</title>
<style type=<\"text/css\">
.even { bgcolor:#5CCDC9; }
.odd { bgcolor:#FFFFFF; }
</style>
</head>
<body>
<?php
echo "<table width=\"50%\" cellpadding=\"2\" cellspacing=\"2\" border=\"1\">";
echo "<tr bgcolor=\"#FFFFFF\">";
$rowcount=0;
for($x=1;$x<=12;$x++){
echo " <td align=\"center\" style=\"width:100px\">".$x."</td>\n";
if ($x%4==0) {
if ($rowcount%2==0){
echo "</tr>";
echo "<tr class=\"even\">\n";
}
else{
echo "</tr>";
echo "<tr class=\"odd\">\n";
}
$rowcount++;
}
}
echo "</tr>";
echo "</table>";
?>
</body>
</html>
now i just dont understand how to write it in PHP I am reading and trying to figure out how to make sense of it. Sorry I am a newbie at this.
You are using $rowcount in your conditional and initialization but you are using $rowscount (with in "s") in your incrementing.
Note: you should really be using CSS for that rather than the bgcolor property.
you have a typo...change $rowscount++ to $rowcount++
The easiest way to do this is with CSS. You can use the nth-child rule to select odd and even rows of the table and colour them differently. That way you don't need the modulus operator if statement you're using.
See this fiddle for an example.
Try this. It's a little bit cleaner and it works:
<html>
<head>
<title> Html Tables</title>
</head>
<body>
<table width="50%" cellpadding="2" cellspacing="2" border="1">
<?php
for($x = 1; $x <= 12; $x++) {
if ($x % 2 == 0) {
echo ' <tr bgcolor="#5CCDC9">', PHP_EOL;
} else {
echo ' <tr bgcolor="#FFFFFF">', PHP_EOL;
}
echo ' <td align="center" style="width:100px">' . $x . '</td>', PHP_EOL;
echo ' </tr>', PHP_EOL;
}
?>
</table>
</body>
</html>

Categories