I'm having problems with a "google like" search engine .
When the page loads, it expects a character in the input box to display inside a div the results.
What can I do to display all the database contents without typing any character in the input box?
Below are the three files:
Index.html
<html>
<head>
<title></title>
</head>
<body>
Caută analizele în baza noastră de date.<br>Taxa de urgență este de 35 Ron
<p align ="right">
<input type="text" id="go" class="field" name="go" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Caută analiza dorită':this.value;" value="Caută analiza dorită"/>
<input type="submit" name="search" id="submit" value="Cauta" /><br/>
<div id="search_query"></div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/instant.js"></script>
</p>
</body>
</html>
Search.php
<?php
$conn = mysql_connect("localhost","user","pass");
mysql_select_db("db");
mysql_set_charset("UTF8", $conn);
if(isset($_POST['search_query'])){
$search_query = mysql_real_escape_string(htmlentities($_POST['search_query']));
$search_query_x = explode(",",$search_query);
foreach($search_query_x as $search_each){
#$x++;
if($x==1)
#$construct.="Den LIKE '%$search_each%'";
else
$construct.="AND Den LIKE '%s$search_each%'";
}
$construct ="SELECT * FROM biochimie1 WHERE $construct";
$run = mysql_query($construct) or die(mysql_error());
$foundnum = mysql_num_rows($run);
// Define $color=1
$color="1";
if ($foundnum==0){
echo "Ne pare rau dar nu există rezultate pentru <strong><font color='red'>$search_query</font></strong></br></br> Vă rugăm să verificați dacă ați scris corect";
}
else{
echo'<p align="center">';
echo "$foundnum \n";
echo ' rezultate găsite în urma căutării!</p>';
echo "<table id='table1' border='0'width='500'>
<tr>
<th width='150' align='center' valign='middle'>Denumire Analize</th>
<th width='50' align='center' valign='middle'>Preț</th>
<th width='100' align='center' valign='middle'>Zile de recoltare</th>
<th width='100' align='center' valign='middle'>Termen de eliberare a rezultatelor</th>
<th width='100' align='center' valign='middle'>Se poate lucra în regim de urgență</th>
</tr></table>";
while($runrows = mysql_fetch_assoc($run))
{
$NrCrt = $runrows ['NrCrt'];
$denanalize = $runrows ['Den'];
$pret = $runrows ['Pret'];
$rec = $runrows ['Rec'];
$rez = $runrows ['Rez'];
$obs = $runrows ['Obs'];
echo "<table border='0'>";
// If $color==1 table row color = #FFC600
if($color==1){
echo "
<tr bgcolor='#efefef'>
<td width='150' align='center' valign='middle'>$denanalize</td>
<td width='50' align='center' valign='middle'>$pret</td>
<td width='100' align='center' valign='middle'>$rec</td>
<td width='100' align='center' valign='middle'>$rez</td>
<td width='100' align='center' valign='middle'>$obs</td>
</tr>
</table>";
// Set $color==2, for switching to other color
$color="2";
}
// When $color not equal 1, use this table row color
else {
echo "<tr bgcolor='#eaeaea'>
<td width='150' align='center' valign='middle'>$denanalize</td>
<td width='50' align='center' valign='middle'>$pret</td>
<td width='100' align='center' valign='middle'>$rec</td>
<td width='100' align='center' valign='middle'>$rez</td>
<td width='100' align='center' valign='middle'>$obs</td>
</tr>";
// Set $color back to 1
$color="1";
}echo "
</table>";
}
}
}
else{
echo 'An ERROR has occured';
}
?>
and instant.js file
$('#go').keyup(function(){
var search_query=$(this).val();
$.post('search.php', {search_query: search_query},function(searchq){
$('#search_query').html(searchq);
});
});
Thank you all in advance.
It will work on load:
$(document).ready(function(){
var search_query=$(this).val();
$.post('search.php', {search_query: search_query},function(searchq){
$('#search_query').html(searchq);
});
Related
I have displayed sql table in html table, made a hyperlink near all fields, when i click it the whole field details should be shows in other page(ie; i show only 2 fields of sql in the table and want to show rest in another page).
admin.php
<?php
$con= mysql_connect("localhost","root","");
mysql_select_db("main",$con);
echo"<form action=\"post\" class=\"form-horizontal\" role=\"form\">";
echo "<table width='700' height='150' onclick='myFun(event)'>";
echo" <tr>
<td width='100' align='center'></td>
<td width='100' align='center'><b><u>NAME</u></b></td>
<td width='100' align='left'><b><u>E-MAIL</u></b></td>
</tr>
";
$result=mysql_query("select NAME,EMAIL from admin order by AID");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo"<td width='100' align='center'><a href='viewadmin.php?name=".$row['NAME']."'>Select</a></td>";
echo"<td width='100' align='center'>".$row['NAME']."</td>";
echo"<td width='100' align='left'>".$row['EMAIL']."</td>";
echo"</tr>";
}
echo"</table>";
echo"</form> ";
?>
viewadmin.php
<?php
$name = $_GET['name'];
$result=mysql_query("SELECT NAME,DOB,MOB,EMAIL, FROM admin WHERE NAME = $name");
if (false === $result) {
echo mysql_error();
}
else {
$row=mysql_fetch_row($result);
}
echo" <form class=\"form-horizontal\" role=\"form\">
<table width='400'>
<tr>
<td align='left'>Name</td>
<td align='left'>".$row['NAME']."</td>
</tr>
<tr>
<td align='left'>E-mail</td>
<td align='left'>".$row['EMAIL']."</td>
</tr>
<tr>
<td align='left'>D.O.B</td>
<td align='left'>".$row['DOB']."</td>
</tr>
<tr>
<td align='left'>Mobile</td>
<td align='left'>".$row['MOBILE']."</td>
</tr>
<tr>
<td align='left'>Photo</td>
<td ><img src='uploads/grumpy.jpg' height='200' width='200'></td>
</tr>
</table>";
echo"</form> ";
?>
do something like this:
admin.php
$result=mysql_query("select NAME,EMAIL from admin order by AID");
while($row=mysql_fetch_array($result)) {
echo "<tr>";
echo"<td width='100' align='center'><a href='viewadmin.php?name=".$row['NAME']."'>Select</a></td>";
echo"<td width='100' align='center'>".$row['NAME']."</td>";
echo"<td width='100' align='left'>".$row['EMAIL']."</td>";
echo"</tr>";
}
echo"</table>";
and in viewadmin.php
$name = $_GET['name'];
$result=mysql_query("SELECT * FROM admin WHERE name = $name");
$row=mysql_fetch_row($result);
echo " <form class=\"form-horizontal\" role=\"form\">
<table width='400'>
<tr>
<td align='left'>".$row['NAME']."</td>
<td align='left'></td>
</tr>
<tr>
<td align='left'>".$row['EMAIL']."</td>
<td align='left'>...";
first rename the html page by php page, then you can pass the primary key or any key of the row from first page to admin page with the help of GET.
for eg:
first.php
<?php
$result=mysql_query("select ID,NAME,EMAIL from admin order by AID"); while($row=mysql_fetch_array($result)){
?><a hre='admin.php?id="$id=<?php $row[0] ?>"'></a>
<?php
}
?>
and in the admin.php page
you can access the value like
echo $_GET['id'];
stop using MySQL and use MySQLi, this code should work
<?php
$db_connect = mysqli_connect('localhost', 'root', 'pass', 'database');
if (mysqli_connect_errno($db_connect)) {
die('Some error occurred during connection to the database');
}
$name = mysqli_real_escape_string($db_connect,$_REQUEST['name']);
if($stmt = mysqli_prepare($db_connect, 'SELECT * FROM admin WHERE name = ?')){
mysqli_stmt_bind_param($stmt, 's', $name);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) !== 0){
$row = mysqli_fetch_assoc($result);
echo "<form class=\"form-horizontal\" role=\"form\">
<table width='400'>
<tr>
<td align='left'>".$row['NAME']."</td>
<td align='left'></td>
</tr>
<tr>
<td align='left'>".$row['EMAIL']."</td>
<td align='left'>..."
}
else{
echo 'not found';
}
}
else{
trigger_error('error:' . mysqli_errno($db_connect) . mysqli_error($db_connect));
}
?>
I have a html page which links to a php function page, with numerous functions on.
It appears that when the refresh button is clicked in Chrome, the text (only from the functions) disappears. Why would this be caused? This is the same with my update buttons which perform a meta refresh...
function Add_Remove_Topics(){
//CONECT TO MYSQL
$username = "root";
$password = "";
$database = "user_test";
$id = ($_SESSION["user_id"]);
$connect = mysql_connect('localhost',$username,$password);
# mysql_select_db($database) or die( "Unable to select database");
//GET VALUES
$sql = "SELECT * FROM topics";
$result = mysql_query($sql);
//echo "<div id='add-remove-topics'>";
//Table headlines - NOT A PHP
echo "<center>Simply type into the fields to change your current topic information - click update once done editing. If you no longer want one of your topics, click the delete button.</center>";
echo "<form action='' method='POST'><table id='artopics'>
<col width='30%'>
<col width='70%'>
<col width='70px'>
<col width='70px'>
<tr height='40px'>
<td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Name</td>
<td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Address</td>
<td style='background-color: #BD4040;'></td>
<td style='background-color: #BD4040;'></td>
</tr>";
// output data of each row
while($row = mysql_fetch_assoc($result))
{
echo
"
<tr height='40px'>
<td align='center' style='font-weight: normal;background-color: white;'><input type='text' name='up_topic_name[{$row['topic_id']}]' value='" . $row['topic_name'] . "'></td>
<td align='center' style='font-weight: normal;background-color: white;'><input type='text' name='up_topic_address[{$row['topic_id']}]' value='" . $row['topic_address'] . "'></td>
<td style='background-color: white;' align='center'><button type='submit' name='updateTopic' value='".$row['topic_id']."'>Update</button></td>
<td style='background-color: white;' align='center'><button type='submit' name='deleteTopic' value='".$row['topic_id']."'>Delete</button></td>
</tr>
";
}
if(isset($_POST['updateTopic'])){
$updateID = $_POST['updateTopic'];
$updateTopicName = $_POST['up_topic_name'][$updateID];
$updateTopicAddress = $_POST['up_topic_address'][$updateID];
$updateTopic = mysql_query("UPDATE topics SET topic_name='$updateTopicName', topic_address='$updateTopicAddress' WHERE topic_id='$updateID'") or die(mysql_error());
echo "<meta http-equiv='refresh' content='0'>";
}
if(isset($_POST['deleteTopic'])){
$deleteID = $_POST['deleteTopic'];
$deleteFromTopics = mysql_query("DELETE FROM topics WHERE topic_id = $deleteID ") or die(mysql_error());
$deleteFromUserTopic = mysql_query("DELETE FROM user_topic WHERE topic_id = $deleteID ") or die(mysql_error());
echo "<meta http-equiv='refresh' content='0'>";
}
echo "</table></form>";
echo "<br><center>To add a new topic, simply fill in the fields below, clicking insert once completed.</center>";
echo "<form action='' method='POST'><table id='artopics'>
<col width='30%'>
<col width='70%'>
<col width='140px'>
<tr height='40px'>
<td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Name</td>
<td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Address</td>
<td style='background-color: #BD4040;'></td>
</tr>";
// output data of each row
echo
"
<tr height='40px'>
<td align='center' style='font-weight: normal;background-color: white;'><input type='text' name='new_topic_name'></td>
<td align='center' style='font-weight: normal;background-color: white;'><input type='text' name='new_topic_address'></td>
<td align='center' style='background-color: white;'><button style='width:130px;' type='submit' name='InsertTopic'>Insert</button></td>
</tr>
";
if(isset($_POST['InsertTopic'])){
$topicName = $_POST['new_topic_name'];
$topicAddress = $_POST['new_topic_address'];
$insertIntoTopics = mysql_query("INSERT INTO topics (topic_name, topic_address) VALUES ('$topicName', '$topicAddress') ") or die(mysql_error());
$grabNewTopicId = mysql_query("SELECT MAX(topic_id) AS topic_id FROM topics") or die(mysql_error());
while($row2 = mysql_fetch_array($grabNewTopicId)) {
$NewTopicId = $row2['topic_id'];
$insertIntoUserTopic = mysql_query("INSERT INTO user_topic (user_id, topic_id) VALUES ('$id', '$NewTopicId') ") or die(mysql_error());
}
echo "<meta http-equiv='refresh' content='0'>";
}
echo "</table></form>";
}
(Screenshots included)
Before refresh/button click
After refresh/button click
I am working on a Zip Code search, and have successfully built the search. Now, I need to have the ability to click on an image map of a map, and hardcode an href to a dynamic URL
So for example, the search works well displaying contact information for a zip code in PA. But if I have an image map, I'd like the ability for it to display the same contact information if I click the image map of PA on the map image.
Here is the code I'm using to do the search, and at the bottom is the code I need help with for the image map.
<form action="search6.php" method="post">
<p><span class="orange16">Zip Code:</span>
<input type="text" name="search_name"> <input type="submit" value="Search" />
</p>
</form>
<br />
<table width="700" border="0">
<?php
if (isset($_POST['search_name'])) {
$search_name = $_POST['search_name'];
if (!empty($search_name)) {
if (strlen($search_name)>=5) {
$query = "SELECT * FROM `search4` WHERE `ZipCode` LIKE '%".mysql_real_escape_string($search_name)."%'";
$query_run = mysql_query($query);
if (mysql_num_rows($query_run)>=1) {
echo "<table width=700' border='0'>";
echo "<tr>";
echo "<td width='700' valign='top'><table width='100%' border='0'>";
echo "<tr>";
echo "<td><p><strong>Results found: </strong></p>";
while ($query_row = mysql_fetch_assoc($query_run)) {{
echo $query_row['ZipCode'].', ';
echo $query_row['ZipCity'].', ';
echo $query_row['ZipState'].'<br><br>';
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo '<span class="productdescription"><p>Office: </p></span></h2>';
echo $query_row['Office'].'<br>';
echo $query_row['Address1'].'<br>';
if(!empty($query_row['Address2'])) // This will skip if the field if it's empty
echo $query_row['Address2'].'<br>';
echo $query_row['City'].', ';
echo $query_row['State'].' ';
echo $query_row['Zip'].'<br>';
echo '<p><strong>Phone Number: </strong></p>';
echo $query_row['Phone'].'<br>';
echo '<p><strong>Fax Number: </strong></p>';
echo $query_row['Fax'].'<br><br>';
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</td>";
//BeginImage display result
$res=mysql_query("select * from Images");
{
echo "<td width='703' align='right' valign='top'>";?> <img src="<?php echo $query_row["Image"]; ?>"> <?php echo "</td>";
echo "</tr>";
}
//EndImage display result
echo ("<table width='700px' border='0' cellpadding='5' cellspacing='1'>
<tr>
<td width='13%' align='left' bgcolor='#C1DDF4'><p><strong>Service Type:</strong></p></td>
<td width='13%' align='left' bgcolor='#C1DDF4'><p><strong>Name:</strong></p></td>
<td width='13%' align='left' bgcolor='#C1DDF4'><p><strong>Phone:</strong></p></td>
<td width='13%' align='left' bgcolor='#C1DDF4'><p><strong>Email:</strong></p></td>
</tr>");
echo ("
<td align='left'><p><strong>Sales</strong></p></td>
<td align='left'><p>$query_row[SalesName]</p></td>
<td align='left'><p>$query_row[SalesPhone]</p></td>
<td align='left'><p><a href='mailto:$query_row[SalesEmail]'class='admin_links'>$query_row[SalesEmail]</p></a></td>
</tr>");
echo ("
<td align='left'><p><strong>Service</strong></p></td>
<td align='left'><p>$query_row[ServiceName]</p></td>
<td align='left'><p>$query_row[ServicePhone]</p></td>
<td align='left'><p><a href='mailto:$query_row[ServiceEmail]'class='admin_links'>$query_row[ServiceEmail]</p></a></td>
</tr>");
echo ("
<td align='left'><p><strong>Service Coordinator</strong></p></td>
<td align='left'><p>$query_row[ServiceCoorName]</p></td>
<td align='left'><p>$query_row[ServiceCoorPhone]</p></td>
<td align='left'><p><a href='mailto:$query_row[ServiceCoorEmail]'class='admin_links'>$query_row[ServiceCoorEmail]</p></a></td>
</tr>");
echo ("</table>");
}
}
}else{
echo 'No results found.';
}
}else{
echo 'Your search must be a 5-digit zip code.';
}
}
}
?>
</table>
This is the code for the Image map I need help with. I'd like to use the "CustClassID" data row for my value of lets say "23-LA".
<?php
$query = "SELECT * FROM search4 WHERE CustClassID = {$_GET['CustClassID']}";
echo ("<table width='600' border='0'>
<tr>
<td align='center'> <img src='images/greymap.png' border='0' usemap='#Map' />
<map name='Map' id='Map'>
<area shape='rect' coords='42,21,136,98' href='search6.php?CustClassID=23-LA' />
</map></td>
</tr>
</table>
");
?>
Basically, is there another way to get the same results as the search, by clicking on specific areas on the imagemap? Please point me in right direction.
hi there i am creating a data grid which loads data from the database and i had even created a search box which prompts the search queries when a user inputs a value into it but now i want to show my search query into the same iframe which is loading the file of data table and when the search box is empty it should show the data table and if a user enters a query it should load the query into it here is my script
<div style="float: right; border: 1px; padding-right: 20px;">
<div class="search">
<input type="text" name="s" maxlength="64" placeholder="Search" id="inputString" onkeyup="lookup(this.value);" />
<img src="images/srch.png" id="srch_btn"/>
</div>
</div>
</div>
</div>
</div>
<table border="0" width="100%" class="myclass" height="30px">
<tr>
<td width="80px" align="center"><font color="black" size="3px">Sr No.</font></td>
<td width="80px" align="center"><font color="black" size="3px">Br No.</font></td>
<td width="180px" align="center"><font color="black" size="3px">Name</font></td>
<td width="200px" align="center"><font color="black" size="3px">Address</font></td>
<td width="120px" align="center"><font color="black" size="3px">City</font></td>
<td width="80px" align="center"><font color="black" size="3px">Pin</font></td>
<td width="80px" align="center"><font color="black" size="3px">Mobile</font></td>
<td width="120px" align="center"><font color="black" size="3px">Email</font></td>
<td width="80px" class="myclass" align="center"><font color="black" size="3px">Actions</font></td>
</tr>
</table>
<div id="suggestions">/****file that shows the search queries**/
<iframe src="record.php" width="1150" height="900" frameBorder="0"></iframe> /** File that loads the data table**/
</div>
here is my search script
$db = new mysqli('localhost', 'root', '', 'mdb');
if(!$db) {
// Show error if we cannot connect.
echo 'ERROR: Could not connect to the database.';
} else {
// Is there a posted query string?
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);
if(strlen($queryString) >0) {
$query = $db->query("SELECT * FROM mdb WHERE (`name` LIKE '%" . $queryString . "%') OR (grno LIKE '%". $queryString ."%')
OR (`address` LIKE '%". $queryString ."%') OR (`city` LIKE '%". $queryString ."%') OR (pin LIKE '%". $queryString ."%')
OR (mobile LIKE '%". $queryString ."%') OR (`email` LIKE'%". $queryString ."%') ORDER BY vouchno LIMIT 8");
if($query) {
echo "<table width='100%'>";
echo "<tr>";
echo "<th align=left>Name</th>";
echo "<th align=left>Address</th>";
echo "<th align=left>City</th>";
echo "<th align=left>Pin</th>";
echo "<th align=right>Mobile</th>";
echo "<th align=left>Email</th>";
echo "</tr>";
while ($result = $query ->fetch_object()) {
echo "<tr>";
echo "<td><span class=\"category\">$result->name</span></td>";
echo "<td>$result->address</td>";
echo "<td>$result->city</td>";
echo "<td>$result->pin</td>";
echo "<td align=right>$result->mobile</td>";
echo "<td>$result->email</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=9>";
echo "<hr/>";
echo "</td>";
echo "<tr>";
$name = $result->name;
if(strlen($name) > 35) {
$name = substr($name, 0, 35) . "...";
}
$description = $result->address;
if(strlen($description) > 80) {
$description = substr($description, 0, 80) . "...";
}
}
echo "</table>";
echo '<span class="seperator"><strong>No Further Records Found</strong> </span><br class="break" />';
} else {
echo 'ERROR: There was a problem with the query.';
}
} else {
echo "record.php";
}
} else {
echo 'There should be no direct access to this script!';
}
}
I think now AJAX can help you I mean. why you don't do somthing like this instead of iframe?
check this
$('#div').load('someFile.php','params');
and for scrolling the div use this in CSS
CSS
#div{
width:500px;
height:300px;
overflow:scroll;
}
here the jsfiddle.
i have this script bellow to open table2 when clicking on the buttom 'more details'
<script>
$(document).ready(function() {
$('#Table1 tr').click(function(event){
$('#Table2').show();
alert($(this).attr('id'));
});
});
</script>
and this my code
<table id= "Table1" width='100%' border='1' cellspacing='0' cellpadding='0'>
$sql2=..
$sql3 = blabla ;
while($row3 =mysql_fetch_array($sql3)){
$sql4 = mysql_query (" SELECT $ww as place FROM data WHERE $ww =".$row3[$ww]." and id_user = ".$userid." ");
$row4 = mysql_fetch_array($sql4) ;
$string = "<blink > here </blink>" ;
$wnumber = $row3[$ww] ;
echo "<tr id= '".$wnumber."'><td style= 'text-align : center ;'>Week ".$row3[$ww]."
</td>" ;
echo "<td >".(int) $row3["percent"] ."% </td>";
echo "<td > "?><?php if($row4['place'] ==
$row3[$ww] and $row2['id'] == $userid ){ echo $string ; } else { echo "";} ;?><?php
"</td>";
echo "<td ><button class='showr'>More Details</button> </td></tr>";
//More Details when clicking on this buttom it open the table2
}
</tr>
</table>
this is second table
<?php
echo "<div id= 'Table2' style= 'display:none;'>";
echo "<table width='100%' border='1' cellspacing='0' cellpadding='0'>";
echo "<th> Week ".$wnumber."</th>";
echo "<th>try2</th>";
echo "<tr ><td>day</td>";
echo "<td>fff</td></tr>";
echo "</table></div>";
?>
*what i have now 5 rows with 5 buttoms .
*what it happen now is when clicking on every bottom it echo same '$wnumber' lets say 6.
however it defers from row to row ,
- script works good with alert of the id of which row is clicked.
- only the last buttom who works with the last id of row.
*what i want is every bottom works with its row id which echo the right '$wnumber'
* what i have tried is (make variable in the div)
echo "<div id= '".$wnumber."' style= 'display:none;'>";
instead of
echo "<div id= 'Table2' style= 'display:none;'>";
but didnt work.
hope its clear and there is solution of it.
EDIT : this source code
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$('#Table1 tr').click(function(event){
$('#Table2').show();
alert($(this).attr('id'));
});
});
</script>
<br />
<table id= "Table1" width='100%' border='1' cellspacing='0' cellpadding='0'>
<th >Weeks</th>
<th ><p></p></th>
<th > Your place</th>
<th > More Details</th>
<tr>
<tr id= '1'><td style= 'text-align : center ;'>Week 1</td><td style= 'text-align :
center ;'>33% </td><td style= 'text-align : center ;'> <td style= 'text-align :
center ;'><button class='showr'>More Details</button></td></tr><tr id= '6'><td
style= 'text-align : center ;'>Week 6</td><td style= 'text-align : center ;'>33%
</td><td style= 'text-align : center ;'> <td style= 'text-align : center
;'><button
class='showr'>More Details</button></td></tr><tr id= '13'><td style= 'text-align:
center ;'>Week 13</td><td style= 'text-align : center ;'>33% </td><td style=
'text-align : center ;'> <blink style= 'color:#990000 ;font-weight: bolder;' > 69
here </blink><td style= 'text-align : center ;'><button class='showr'>More
Details</button></td></tr></tr>
</table>
<br />
<div id= 'Table2' style= 'display:none;'><table width='100%' border='1'
cellspacing='0' cellpadding='0'><th> Week 13</th><th>try2</th><tr ><td>day</td>
<td>fff</td></tr></table></div>
<br /><br /> <br />
I tried your html code after correcting the missing td, tr.
And then clicking on each row / button displays the div.
Ensure you form proper html code in your php echo
Try something like this:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$('#Table1 tr').click(function(event){
$('#details').find('#week' + $(this).attr('id')).show();
// alert($(this).attr('id'));
});
});
</script>
<?php
$result = mysql_query($sql);
// save the results to an array so you can loop them twice
while($row = mysql_fetch_array($result)) :
$rows[] = $row;
endwhile;
?>
<table id="table1">
<tr>
<th>heading</th>
<th>heading</th>
<th>heading</th>
<th>heading</th>
</tr>
<?php foreach ($rows as $row) : // loop #1 to output the main table ?>
<tr id="<?php echo $row['ww'] ?>">
<td>value</td>
<td>value</td>
<td>value</td>
<td><button type="button">More details</button></td>
</tr>
<?php endforeach; ?>
</table> <!-- end table 1 -->
<div id="details">
<?php foreach ($rows as $row) : // loop #2 to output a table for each set of details ?>
<table id="week<?php echo $row['ww'] ?>" style="display: none">
<tr>
<th>Week <?php echo $row['ww'] ?></th>
<th>try2</th>
</tr>
<tr>
<td>value</td>
<td>value</td>
</tr>
</table>
<?php endforeach; ?>
</div> <!-- end details -->
I copy / pasted your code into a jsFiddle and it seems to work as expected. Clicking on the row sends an alert with the correct ID.
http://jsfiddle.net/JeeNN/
Is there something I'm missing in your intent here?
Note : Best practice would be to skip the inline CSS and add external styling for your tables. Also, valid HTML is a must.
Update:
I went ahead and formatted the php code you posted so it's a bit easier to read. There's some variables that aren't defined and a couple other issues, but I'm sure you have it correct in your php file.
I think that you're going to want to run the loop a second time to create the second table. In this second loop, echo out the second table once per loop - you'll end up with a bunch of tables (one per row plus the first table). Then simply swap the visibility of the tables as user's click each id.
Is that what you're looking for? If not, perhaps try to rephrase your question.
Here's that formatted code:
<table id="Table1" width='100%' border='1' cellspacing='0' cellpadding='0'>
<?php
$sql2 = [..];
$sql3 = [..];
$ww = ?;
while($row3 = mysql_fetch_array($sql3)){
$sql4 = mysql_query ("SELECT $ww as place FROM data WHERE $ww =".$row3[$ww]." and id_user = ".$userid." ");
$row4 = mysql_fetch_array($sql4) ;
$string = "<blink> here </blink>" ;
$wnumber = $row3[$ww];
echo "<tr id='".$wnumber."'>";
echo "<td style='text-align:center;'>Week ".$row3[$ww]."</td>";
echo "<td>".(int) $row3["percent"] ."% </td>";
echo "<td>":
if($row4['place'] == $row3[$ww] and $row2['id'] == $userid ){
echo $string;
} else {
echo " ";
}
echo "</td>";
//More Details when clicking on this buttom it open the table2
echo "<td ><button class='showr'>More Details</button></td>";
echo "</tr>";
} ?>
</table>
<div id= 'Table2' style= 'display:none;'>
<table width='100%' border='1' cellspacing='0' cellpadding='0'>
<tr>
<th> Week <?php echo $wnumber; ?></th>
<th>try2</th>
</tr>
<tr>
<td>day</td>
<td>fff</td>
</tr>
</table>
</div>