I want to block the technician holidays in the calendar, dates are scheduled in the SQL server.
So far I have managed to block the days before the current day.
I wanted to do the same but with the dates of the holidays.
dayRender: function (date, cell) {
if (moment().diff(date, 'days') > 0) {
cell.css("background-color", "#F5F5F5");
$(cell).addClass('disabled');
}
},
I appreciate all the help, thanks in advance.
EDIT:
This is what I want to do:
https://codepen.io/anon/pen/xyxeJm
EDIT 2
my events code:
<script>
var url = window.location.href;
var recortar = url.indexOf("agenda=");
var tec = url.substr(recortar + 7);
var table = $('#example').DataTable({
"iDisplayLength": 5,
"bLengthChange": false,
pagingType: "simple",
"order": [[ 1, 'asc' ]],
});
$('#example tbody').on('click', 'tr', function () {
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
} else {
table.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
});
<?php
if(isset($_GET['agenda'])){
$tec = selecttec($_GET['agenda']);
// curativas
$sql = "SELECT * FROM npedido WHERE tec1= '$tec' OR tec2= '$tec' OR responsable = '$tec' ";
$stmt2 = sqlsrv_query( $conn, $sql);
// agendamentos
$sql_agenda = "SELECT * FROM nagenda WHERE tec1= '$tec' OR tec2= '$tec'";
$stmt_agenda = sqlsrv_query( $conn, $sql_agenda);
?>
var name = "<?php echo utf8_encode($tec); ?>";
table.search(name).draw();
$("#tec_name").val(name);
$( "#example tr").each(function() {
if(this.id == tec){
$(this).addClass('selected');
}
});
var tec = "<?php echo $tec; ?>";
var events = function () {
return [
// curativas agenda
<?php while($row = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_NUMERIC)) {
if(strlen(str_replace(' ', '', $row[7])) < 15){ $start=date('h:i Y-m-d',strtotime(str_replace("18", "2018", substr($row[7], -16, 8)))) ;}else{ $start= date('Y-m-d H:i',strtotime($row[7])) ; }
if(strlen(str_replace(' ', '', $row[7])) < 15){ $end = date('h:i Y-m-d',strtotime(str_replace("18", "2018", substr($row[7], -16, 8)))) ;}else{ $end = date('Y-m-d H:i ',strtotime($row[7])) ; }
$start = substr($start,0,10)."T". substr($start,10);
$start = str_replace(" ","",$start);
$end = substr($end,0,10)."T". substr($end,10);
$end = str_replace(" ","",$end);
?> {
id: '<?php echo $row[0] ?>',
title: '<?php echo "Curativa - ".utf8_encode($row[8]).""; ?>',
start: '<?php echo $start; ?>',
end: '<?php echo $end; ?>',
color: '#0FB1DA',
},
<?php }?>
// agendamento
<?php while($row = sqlsrv_fetch_array($stmt_agenda, SQLSRV_FETCH_NUMERIC)) {?>
{
id: '<?php echo $row[0]; ?>',
title: '<?php echo "Agendamento - ".utf8_encode($row[4]).""; ?>',
start: '<?php echo $row[6]; ?>',
end: '<?php echo $row[7]; ?>',
dow: [1,4],
color: '<?php echo $row[10];?>',
},
<?php }?>
];
}
table.search("").draw();
<?php }else{?>
$('#example tbody tr:eq(0)').click();
<?php }sqlsrv_close($conn);?>
</script>
i just added the code and just set the background color, but i whant to disable the click funcion on those days...how i do that?
Code added:
// ferias
<?php while($row = sqlsrv_fetch_array($stmt_ferias, SQLSRV_FETCH_NUMERIC)) {?>
{
id: '<?php echo $row[0]; ?>',
title: '<?php echo "FĂ©rias - ".utf8_encode($row[2]).""; ?>',
start: '<?php echo $row[3]; ?>',
end: '<?php echo $row[4]; ?>',
dow: [1,4],
rendering: 'background',
color: '#ff9f89'
},
<?php }?>
Related
I have this pagination written in php that works fine, but I want to use ajax to implement this so that the pages won't have to reload every time a link is clicked. I have gone through previous questions asked on this same topic here on SO and other blogs but I'm not getting it right.
Here's my pagination code in nav_tbl.php
if(!empty($_POST['rownum'])){
$rowno = $_POST['rownum'];
//get current page
$currentpage = isset($_POST['currentpage']) ? $_POST['currentpage'] : 1;
$no_of_records_per_page = $rowno;
$setoff = ($currentpage - 1) * $no_of_records_per_page;
//get total number of records in database
$sqlcount = "SELECT *FROM evatbl WHERE RegNo = ?";
$stmt = $con->prepare($sqlcount);
$stmt->bind_param("s", $pregno);
$pregno = $_SESSION['regno'];
$stmt->execute();
$result = $stmt->get_result();
$num_rows = $result->num_rows;
$totalpages = ceil($num_rows/$no_of_records_per_page);
//query for pagination
$sqllimit = "SELECT *FROM evatbl WHERE RegNo = ? ORDER BY CourseTitle LIMIT $setoff, $no_of_records_per_page";
if ($stmt = $con->prepare($sqllimit))
{
$stmt = $con->prepare($sqllimit);
$stmt->bind_param("s", $pregno);
$pregno = $_SESSION['regno'];
$stmt->execute();
$result = $stmt->get_result();
$num_rows = $result->num_rows;
if ($num_rows>0)
{
$count = 1;
echo "<table id='t01' class='table' width='100%'>
<tr id='tblhead'>
<th>SN</th>
<th>Course Title</th>
<th>Course Code</th>
<th>Credit Unit</th>
<th>Course Lecturer</th>
<th>Rating(%)</th>
</tr>";
while($row = $result->fetch_assoc())
{
$ccode = $row['CourseCode'];
$ctitle = $row['CourseTitle'];
$cunit = $row['CreditUnit'];
$clec = $row['CourseLecturer'];
$crate = $row['Rating'];
echo "
<tr>
<td>$count</td>
<td>$ctitle</td>
<td>$ccode</td>
<td>$cunit</td>
<td>$clec</td>
<td>$crate</td>
</tr>";
$count++;
}
echo "</table>";
}
else{
echo "<p style='color:darkred;margin-bottom:0;'>Oops! No records found.</p>";
}
?><br>
<div class="nav_div" id="nav_div">
<label for="navno">Navigate pages: </label>
<?php
//First Page Button
if($currentpage > 1)
{
echo "<a class='nav_a' id='first' href='view_eva.php?limit=".$rowno."¤tpage=".(1)."' title='First'><<</a>";
}
//Previous Page Button
if($currentpage >= 2)
{
echo "<a class='nav_a' id='prev' href='view_eva.php?limit=".$rowno."¤tpage=".($currentpage - 1)."' title='Previous'><</a>";
}
?>
<select class='navno' name='navno' id='navno' onchange="pageNav(this)">
<?php
//Link to available number of pages with select drop-down
for($i = 1; $i <= $totalpages; $i++)
{
echo "<option class='bold'";
if ($currentpage==$i)
{
echo "selected";
}
echo " value='view_eva.php?limit=".$rowno."¤tpage=".$i."'>".$i."</option>";
}
?>
</select>
<?php
//Next Page Button
if($currentpage < $totalpages)
{
echo "<a class='nav_a' id='next' href='view_eva.php?limit=".$rowno."¤tpage=".($currentpage + 1)."' title='Next'>></a>";
}
//Last Page Button
if($currentpage <= $totalpages - 1)
{
echo "<a class='nav_a' id='last' href='view_eva.php?limit=".$rowno."¤tpage=".($currentpage = $totalpages)."' title='Last'>>></a>";
}
?>
</div>
<?php
}
else
{
echo "<p style='color:darkred;margin-bottom:0;'>Oops! No records found.</p>";
}
?>
And here's the Ajax code to set number of rows to display per page using a select dropdown option
$(document).on('change', '#rowno', function(e){
e.preventDefault();
var rownum = $('#rowno').val();
if($('#check').is(":checked")) {
$('#check').prop('checked',false);
}
$.ajax({
type: 'POST',
url: 'navtbl.php',
cache: false,
async: false,
data: {
rownum : rownum
},
success: function(datalog){
console.log(datalog);
$("#display_table").html(datalog).show();
},
error: function(datalog){
swal("Oops.", "An error occured! Unable to fetch result.", "error");
}
});
});
// Call Function to load and display table on Page Load
function Ini_loadTable(){
var rownum = $('#rowno').val();
$.ajax({
type: 'POST',
url: 'navtbl.php',
cache: false,
async: false,
data: {
rownum : rownum
},
success: function(datalog){
console.log(datalog);
$("#display_table").html(datalog).show();
},
error: function(datalog){
swal("Oops.", "An error occured! Unable to fetch result.", "error");
}
});
}
Please, any help will be much appreciated. Thank you.
When data is fetched on auto Load More, another JavaScript (which is given below) stops working.
Even when it shows the 1st page default it works there but after scrolling down it does not work.
Updated with more explaination with index and getdata file. I'm fetching data like mentioned in the below codes.
index.php
<div>
<?php include('getdata.php'); ?>
</div>
<script>
$(document).ready(function(){
var is_ajaxed = false;
function getresult(url) {
$.ajax({
url: url,
type: "GET",
data: {rowcount:$("#rowcount").val()+1},
beforeSend: function(){
$('#loader-icon').show();
},
complete: function(){
$('#loader-icon').hide();
},
success: function(data){
$("#faq-result").append(data);
},
error: function(){}
});
}
$(window).scroll(function(){
if ($(window).scrollTop() >= ($(document).height() - $(window).height()-900) && is_ajaxed == false){
if($(".pagenum").val() <= $(".total-page").val()) {
var pagenum = parseInt($(".pagenum").val()) + 1;
var pname = "<?php echo $pgianame; ?>";
var sname = "<?php echo $stianame; ?>";
getresult('sellers_forum_page_posts_getresult.php?page='+pagenum+'&pname='+pname+'&sname='+sname);
is_ajaxed = true
}
}
});
});
</script>
getdata.php
<?php
include ('db.php');
$perPage = 10;
$sql = "select * from posts where status = 1";
$faq3x = $dba3->query($sql);
$allrowscount = mysqli_num_rows($faq3x);
$pages = ceil($allrowscount/$perPage);
$page = 1;
if(!empty($_GET["page"])) {
$page = $_GET["page"];
}
$start = ($page-1)*$perPage;
if($start <= 0) $start = 0;
$sqlsC = "select * from posts where status = 1 ";
$query = $sql . " limit " . $start . "," . $perPage;
$faq3xsC = $dba3->query($query);
echo '<input type="text" class="pagenum" value="'.$page.'" />';
echo '<input type="text" class="total-page" value="'.$pages.'" />';
while ($faqC = $faq3xsC->fetch_assoc()) {
$id = $faqC['id'];
$ext = ".doc";
$name = $id.$ext;
$filepath = "blog/posts/";
$filename = $filepath.$name;
if(file_exists($filename)) {
if(($fhP = fopen($filename, 'r')) !== false ) {
$headersxP = fread($fhP, 999000);
$commentsPP = nl2br($headersxP);
$countxP = 0;
$fhxP = fopen($filename, 'r');
while(!feof($fhxP)){
$frxP = fread($fhxP, 999999);
$countxP += strlen($frxP);
}
fclose($fhxP);
?>
<?php echo substr($commentsPP, 0, 511); ?>
<span id="dots_<?php echo $idPost; ?>">...</span>
<span id="more_<?php echo $idPost; ?>" style="">
<?php echo substr($commentsPP, 512, 999999); ?>
</span>
<a onclick="myFunctionx_<?php echo $id; ?>()"
id="myBtn_<?php echo $id; ?>"> See More <i class="fa fa-long-arrow-right"></i></a>
<script>
function myFunctionx_<?php echo $id; ?>() {
var dots_<?php echo $id; ?> = document.getElementById("dots_<?php echo $id; ?>");
var moreText_<?php echo $id; ?> = document.getElementById("more_<?php echo $id; ?>");
var btnText_<?php echo $id; ?> = document.getElementById("myBtn_<?php echo $id; ?>");
if (dots_<?php echo $id; ?>.style.display === "none") {
dots_<?php echo $id; ?>.style.display = "inline";
btnText_<?php echo $id; ?>.innerHTML = " See more <i class='fa fa-long-arrow-right'></i>";
moreText_<?php echo $id; ?>.style.display = "none";
} else {
dots_<?php echo $id; ?>.style.display = "none";
btnText_<?php echo $id; ?>.innerHTML = " <i class='fa fa-long-arrow-left'></i> See less";
moreText_<?php echo $id; ?>.style.display = "inline";
}
}
</script>
<?php } ?>
I am not very proficient with AJAX requests, but I have this simple script in my header:
function refresh_div() {
jQuery.ajax({
url:'progress.php?game=<?php echo $gamename; ?>',
type:'POST',
success:function(results) {
jQuery(".middle").html(results);
}
});
}
t = setInterval(refresh_div,1000);
and then in the progress.php file I have quite a few mysql requests from my db, the purpose of which is to refresh the page with this php data every second. This works fine most times, but what I am finding however is that whenever I call this script a few times back to back simultaneously, I am getting timeout errors and the page wont load. This is all for a browser based game which includes a timer which counts down every second so it is important I find something that will be stable executing php requests every second.
Added progress.php:
<?php
session_start();
$username = $_SESSION["username"];
$gamename = $_GET['game'];
require "../connections/games.php";
$newgamesql = "SELECT * FROM gamelist WHERE name = '$gamename'";
$newgamequery = mysql_query($newgamesql, $gamesdbhandle);
$newgamecount = mysql_num_rows($newgamequery);
$gamenamenospaces = str_replace(' ','_', $gamename);
$gamestatus = mysql_result($newgamequery, 0, 'status');
$numberofplayers = mysql_result($newgamequery, 0, 'numberofplayers');
$player1 = mysql_result($newgamequery, 0, 'player1');
$player2 = mysql_result($newgamequery, 0, 'player2');
$player3 = mysql_result($newgamequery, 0, 'player3');
$player4 = mysql_result($newgamequery, 0, 'player4');
$player5 = mysql_result($newgamequery, 0, 'player5');
$player6 = mysql_result($newgamequery, 0, 'player6');
$player7 = mysql_result($newgamequery, 0, 'player7');
$player8 = mysql_result($newgamequery, 0, 'player8');
$rounds = mysql_result($newgamequery, 0, 'rounds');
$currentround = mysql_result($newgamequery, 0, 'currentround');
$gametimestamp = mysql_result($newgamequery, 0, 'actualtime');
$headmaster = mysql_result($newgamequery, 0, 'headmaster');
$roundtime = mysql_result($newgamequery, 0, 'time');
$activeplayers = mysql_result($newgamequery, 0, 'activeplayers');
$numberofplayers = mysql_result($newgamequery, 0, 'numberofplayers');
$roundtimeproper = strtotime("H:i:s" , $roundtime);
//Round time conversion goes here:
//The specific table query for the individual rounds
$thegamesql = "SELECT * FROM `$gamenamenospaces` WHERE id='$currentround' LIMIT 1";
$thegamequery = mysql_query($thegamesql, $gamesdbhandle);
$echocurrentround = mysql_num_rows($thegamequery) + 1;
$thisroundplayer1 = mysql_result($thegamequery, 0, "player1card");
$thisroundplayer2 = mysql_result($thegamequery, 0, "player2card");
$thisroundplayer3 = mysql_result($thegamequery, 0, "player3card");
$thisroundplayer4 = mysql_result($thegamequery, 0, "player4card");
$thisroundplayer5 = mysql_result($thegamequery, 0, "player5card");
$thisroundplayer6 = mysql_result($thegamequery, 0, "player6card");
$thisroundplayer7 = mysql_result($thegamequery, 0, "player7card");
$thisroundplayer8 = mysql_result($thegamequery, 0, "player8card");
$thisroundwhitecard = mysql_result($thegamequery, 0, "whitecard");
$thisroundwhitecard = str_replace("_", "_____", $thisroundwhitecard);
$thisroundtime = mysql_result($thegamequery, 0, "timestamp");
$now = new DateTime();
$target = DateTime::createFromFormat('H:i:s', $gametimestamp);
$difference = $now->diff($target);
$difference = $difference->format("%I:%S");
//Display the timer
echo '<div id="timer">';
echo 'TIME:';
echo '<br />';
echo $difference;
echo '</div>';
//echo $timer;
//State which round
echo '<div id="toptitle">';
echo $gamename;
echo ' - ';
echo 'Round ';
echo $currentround;
echo '</div>';
if ($gamestatus == 'active') {
//Game is live, display things
echo '<div id="placeholderplaceholder">';
//The card submit bar
if (!empty($player1)) {
if (!empty($player1card)) {
echo '<div class="blackcardplaceholderplaced">';
}else{
echo '<div class="blackcardplaceholder">';
}
echo 'P1';
echo '</div>';
}
if (!empty($player2)) {
if (!empty($player2card)) {
echo '<div class="blackcardplaceholderplaced">';
}else{
echo '<div class="blackcardplaceholder">';
}
echo 'P2';
echo '</div>';
}
if (!empty($player3)) {
if (!empty($player3card)) {
echo '<div class="blackcardplaceholderplaced">';
}else{
echo '<div class="blackcardplaceholder">';
}
echo 'P3';
echo '</div>';
}
if (!empty($player4)) {
if (!empty($player4card)) {
echo '<div class="blackcardplaceholderplaced">';
}else{
echo '<div class="blackcardplaceholder">';
}
echo 'P4';
echo '</div>';
}
if (!empty($player5)) {
if (!empty($player5card)) {
echo '<div class="blackcardplaceholderplaced">';
}else{
echo '<div class="blackcardplaceholder">';
}
echo 'P5';
echo '</div>';
}
if (!empty($player6)) {
if (!empty($player6card)) {
echo '<div class="blackcardplaceholderplaced">';
}else{
echo '<div class="blackcardplaceholder">';
}
echo 'P6';
echo '</div>';
}
if (!empty($player7)) {
if (!empty($player7card)) {
echo '<div class="blackcardplaceholderplaced">';
}else{
echo '<div class="blackcardplaceholder">';
}
echo 'P7';
echo '</div>';
}
if (!empty($player8)) {
if (!empty($player8card)) {
echo '<div class="blackcardplaceholderplaced">';
}else{
echo '<div class="blackcardplaceholder">';
}
echo 'P8';
echo '</div>';
}
echo '</div>';
if ($headmaster == $username) {
//You are the headmaster, show the judge view
echo '<br />Your turn to <div id="headmaster">Judge</div><br /><br />';
if(!empty($thisroundwhitecard)) {
echo '<div class="bigwhitecard">';
echo $thisroundwhitecard;
echo '</div>';
}
}else{
//You are not the headmaster, show the player view
//Randomly generate 5 cards
require '../dbauth/cards.inc.php';
$cardsdbhandle = mysql_connect(
$cardshostname,
$cardsusername,
$cardspassword
) or die("INVALID USERNAME OR PASSWORD");
$cardsselected = mysql_select_db($cardsdbname, $cardsdbhandle);
$newblackcardsql = "SELECT * FROM black LIMIT 5";
$newblackcardquery = mysql_query($newblackcardsql, $cardsdbhandle);
$newblackcardcount = mysql_num_rows($newblackcardquery);
$c = 0;
if ($newblackcardcount > 0) {
echo '<div class="blackcardscroller">';
echo '<div class="blackcardscrollerinner">';
while ($c < $newblackcardcount) {
$blackcardid = mysql_result($newblackcardquery, $c, "id");
$blackcardtext = mysql_result($newblackcardquery, $c, "text");
echo '<div class="gameblackcard">';
echo $blackcardtext;
echo '<div class="blackcardbtn" id="';
echo $blackcardid;
echo '">PICK CARD</div>';
echo '</div>';
$c++;
}
echo '</div>';
echo '</div>';
}else{
//There are no black cards
echo 'There are no black cards to pick from! Something went wrong...';
}
echo '<br />Judge: <br />';
echo '<div id="headmaster">';
echo $headmaster;
echo '</div>';
if(!empty($thisroundwhitecard)) {
echo '<div class="bigwhitecard">';
echo $thisroundwhitecard;
echo '</div>';
}
}
}
?>
You should not use setInterval... Do this instead:
function refresh_div() {
jQuery.ajax({
url:'progress.php?game=<?php echo $gamename; ?>',
type:'POST',
success:function(results) {
jQuery(".middle").html(results);
setTimeout(function(){
refresh_div();
}, 1000);
}
});
}
refresh_div();
I think you problem in this string
t = setInterval(refresh_div,1000);
You get queries from servers each second, so after 10 seconds there are 10 query in row and this can cause timeouts because server too busy to answer and also it may be some other problems because to much concurrent queries. So i suggest you try use promise(), take look in this post:
in JavaScript, how to wrap a promise in timeout?
I have a page with buttons that a user clicks on.
When the button gets clicked I need it to change the image and call a php script to save the change.
I somehow can't get the button to do both, it changes the pic but won't call the php.
CODE:
<?php
$db = new PDO('mysql:host=localhost;dbname=MySettings;charset=utf8mb4', 'TestUser', '1234567890');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
try {
$mytable = $_SESSION["SESS_myuserid"];
if($mytable == null)
{$url='login.php';
echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';}
$stmt = $db->prepare("SELECT * FROM ".$mytable);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$conn = null;
echo'<script>';
$count = count($result);
for ($i = 0; $i < $count; $i++) {
$TheId = $result[$i]['Id'];
$TheFunction = $result[$i]['TheFunction'];
$TheSetting = $result[$i]['TheSetting'];
if ($TheSetting == "0"){
echo 'var newsrc'.$TheId.' = "on.jpg";';
}
else{
echo 'var newsrc'.$TheId.' = "off.jpg";';
}
echo 'function changeImage'.$TheId.'() {';
echo 'if ( newsrc'.$TheId.' == "off.jpg" ) {';
echo "document.getElementById('pic".$TheId."').src = '/images/Boff.png';";
echo "$('#newCode').load('Monitor.php?id=".$TheId."&Table=".$mytable."');";
echo 'newsrc'.$TheId.' = "on.jpg";';
echo '}';
echo 'else {';
echo "document.getElementById('pic".$TheId."').src = '/images/Bon.png';";
echo "$('#newCode').load('Monitor.php?id=".$TheId."&Table=".$mytable."');";
echo 'newsrc'.$TheId.' = "off.jpg";';
echo '}';
echo '}';
}
echo'</script>';
$myLeft=0;
$myTop=0;
$myRow=0;
for ($i = 0; $i < $count; $i++) {
$TheId = $result[$i]['Id'];
$TheFunction = $result[$i]['TheFunction'];
$TheSetting = $result[$i]['TheSetting'];
if ($TheSetting == "0"){
$ThePic = "images/Boff.png";
}
else{
$ThePic = "images/Bon.png";
}
echo '<div>';
echo ' ';
echo '</div>';
$myTop=$myTop + 30;
$myRow=$myRow + 1;
if ($myRow == 12){
$myRow=0;
$myTop=0;
if ($myLeft == 0){
$myLeft = 292;
}
else {
$myLeft = 615;
}
}
}
}
catch(PDOException $e) {
$url='login.php';
echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';
}
?>
Fixed :
Changed the script function. Added a hidden div to be called to load the php which outputs nothing so stays invisible.
echo'<script>';
$count = count($result);
for ($i = 0; $i < $count; $i++) {
$TheId = $result[$i]['Id'];
$TheFunction = $result[$i]['TheFunction'];
$TheSetting = $result[$i]['TheSetting'];
if ($TheSetting == "0"){
echo 'var newsrc'.$TheId.' = "on.jpg";';
}
else{
echo 'var newsrc'.$TheId.' = "off.jpg";';
}
echo 'function changeImage'.$TheId.'() {';
echo 'if ( newsrc'.$TheId.' == "off.jpg" ) {';
echo "document.getElementById('pic".$TheId."').src = '/images/Boff.png';";
echo 'newsrc'.$TheId.' = "on.jpg";';
echo '}';
echo 'else {';
echo "document.getElementById('pic".$TheId."').src = '/images/Bon.png';";
echo 'newsrc'.$TheId.' = "off.jpg";';
echo '}';
echo' $("#HtmlConvo").load("Monitor.php?id='.$TheId.'&Table='.$mytable.'");';
echo '}';
}
echo'</script>';
i have a image database and a search form , so i want to show the images in next page and i have to use javascript to show (openlayer library)
i wrote this code`
<?php mysql_connect('localhost','root',"");
mysql_select_db('geo-image');
$am=0 ;
$sql="SELECT * FROM images WHERE satellite='".$_POST["satellite"]."'";
$result=mysql_query($sql)or die(mysql_error());
$count = mysql_num_rows($result);
for ($i=1; $i<=$count; $i++)
{
$rows = mysql_fetch_array($result);
${'a'.$i}=$rows['xpos'];
${'b'.$i}=$rows['ypos'];
${'c'.$i}=$rows['width'];
${'d'.$i}=$rows['height'];
${'e'.$i}=$rows['xoffset'];
${'f'.$i}=$rows['yoffset'];
${'g'.$i}=$rows['rotation'];
${'h'.$i}=$rows['resolution'];
${'i'.$i}=$rows['name'];
${'j'.$i}=$rows['url'];
}
?>
and the javascript code:
var a1="<?php echo $a1?>";
var b1="<?php echo $b1?>";
var c1="<?php echo $c1?>";
var d1="<?php echo $d1?>";
var e1="<?php echo $e1?>";
var f1="<?php echo $f1?>";
var g1="<?php echo $g1?>";
var h1="<?php echo $h1?>";
var a2="<?php echo $a2?>";
var b2="<?php echo $b2?>";
var c2="<?php echo $c2?>";
var d2="<?php echo $d2?>";
var e2="<?php echo $e2?>";
var f2="<?php echo $f2?>";
var g2="<?php echo $g2?>";
var h2="<?php echo $h2?>";
var l1=340;
var l2=346;
if("<?php echo $count?>"==1){
anArray[anArray.length] = [ a1,b1, c1, d1, e1, f1, g1, h1,l1,"image/<?php echo $j1?>.jpg"];
} else if("<?php echo $count?>"==2){
anArray[anArray.length] = [ a1,b1, c1, d1, e1, f1, g1, h1,l1,"image/<?php echo $j1?>.jpg"];
anArray[anArray.length] = [ a2,b2, c2, d2, e2, f2, g2, h2,l2,"image/<?php echo $j2?>.jpg"];
} else if("<?php echo $count?>"==3){
anArray[anArray.length] = [ a1,b1, c1, d1, e1, f1, g1, h1,l1,"image/<?php echo $j1?>.jpg"];
anArray[anArray.length] = [ a2,b2, c2, d2, e2, f2, g2, h2,l2,"image/<?php echo $j2?>.jpg"];
anArray[anArray.length] = [ a3,b3, c3, d3, e3, f3, g3, h3,l3,"image/<?php echo $j3?>.jpg"];
}
when in the database search i have two result this code work properly but when the result is more than two the code doesnt work, i know the problem is about my variable in javascript but i have no idea to fix it , can any one help me to solve it??
PHP :
<?php mysql_connect('localhost','root',"");
mysql_select_db('geo-image');
$ARR_DATA = array();
$sql="SELECT * FROM images WHERE satellite='".$_POST["satellite"]."'";
$result=mysql_query($sql)or die(mysql_error());
while($rows = mysql_fetch_array($result)
{
$arr_temp['a'] = $rows['xpos'];
$arr_temp['b'] = $rows['ypos'];
$arr_temp['c'] = $rows['width'];
$arr_temp['d'] = $rows['height'];
$arr_temp['e'] = $rows['xoffset'];
$arr_temp['f'] = $rows['yoffset'];
$arr_temp['g'] = $rows['rotation'];
$arr_temp['h'] = $rows['resolution'];
$arr_temp['i'] = $rows['name'];
$arr_temp['j'] = $rows['url'];
$ARR_DATA[] = $arr_temp;
}
$count = count($ARR_DATA);
?>
JS :
<script>
<?php
foreach($ARR_DATA as $key=>$arr_temp)
{
?>
var a = "<?php echo $arr_temp['a'];?>";
var b = "<?php echo $arr_temp['b'];?>";
var c = "<?php echo $arr_temp['c'];?>";
var d = "<?php echo $arr_temp['d'];?>";
var e = "<?php echo $arr_temp['e'];?>";
var f = "<?php echo $arr_temp['f'];?>";
var g = "<?php echo $arr_temp['g'];?>";
var h = "<?php echo $arr_temp['h'];?>";
var i = "<?php echo $arr_temp['i'];?>";
var j = "<?php echo $arr_temp['j'];?>";
var img = "image/"+j+".jpg";
anArray[<?php echo $key;?>] = [ a, b, c, d, e, f, g, h, i, j];
<?php
}
?>
</script>
try
in php code
<?php mysql_connect('localhost','root',"");
mysql_select_db('geo-image');
$am=0 ;
$sql="SELECT * FROM images WHERE satellite='".$_POST["satellite"]."'";
$result=mysql_query($sql)or die(mysql_error());
$count = mysql_num_rows($result);
$images = array();
for ($i=1; $i<=$count; $i++)
{
$rows = mysql_fetch_array($result);
$images[] = $rows;
}
?>
in js code
<?php
foreach ($images as $image){
?>
anArray[anArray.length] = [ "<?php echo $image['xpos'];?>","<?php echo $image['ypos'];?>", "<?php echo $image['width'];?>", "<?php echo $image['height'];?>", "<?php echo $image['xoffset'];?>", "<?php echo $image['yoffset'];?>", "<?php echo $image['rotation'];?>", "<?php echo $image['resolution'];?>","<?php echo $image['name'];?>","image/<?php echo $image['url']?>.jpg"];
<?php
}
?>
the safiest way to communicate between php and js is to use json in my opinion. additionnaly, I would suggest you to use pdo or mysqli instead of mysql for the communication with your database.