So I have this problem where i define a table(may not be the most effective way of doing it but that is not the problem) which works out just fine for me. However, when I try to change the contents of tabledata assigned with its ID, nothing happnes, no new image is displayed instead of the one that has been assigned to id at the creation of the table.
var idArray = window.idArray;
var imgArray = window.imgArray;
var prArray = window.prArray;
var screenH = screen.height/2.1;
var screenW = screen.width/2.1;
var tdW;
var tdH;
function createTables(){
if(idArray.length>2){
rows=2;
cols=2;
tdW='100%';
tdH='100%';
}else if(idArray.length==2){
rows=1;
cols=2;
tdW='100%';
tdH='100%';
}else if(idArray.length==1){
tdW='100%';
tdH='100%';
}
var table = '';
for(var r=0;r<rows;r++){
table += '<tr>';
for(var c=0;c<cols;c++){
table += '<td style="max-width:'+tdW+';max-height:'+tdH+';" id="c'+r+''+c+'">';
table += '<img style="max-height:'+screenH+';max-width:'+screenW+';" src="'+imgArray[2]+'"';
table += '</td>';
}
table += '</tr>';
}
document.write("<table>"+table+"</table>");
}
function addImage(r,c){
document.getElementById('c'+r+''+c).innerHTML('<img style="max-height:'+screenH+';max-width:'+screenW+';" src="'+imgArray[1]+'"');
}
everything works fine untill I try to change the image in particular column of the particular row so it would begin displaying instead of the first picture assigned to the cell with its' id. id of the cell: 'c00' (the problem is the function addImage().. this "innerHTML" stuff doesnt work for me..).
I really don't get it...
Thats the main php file:
<?php
include "includes/connect.php";
?>
<html>
<head>
<link type="text/css" rel="stylesheet" href="includes/style.css">
<title>Cablex-T OG</title>
</head>
<?php
$idArray = array();
$imgArray = array();
$prArray = array();
fillArrays();
?>
<script>
var idArray = <?php echo json_encode($idArray) ?>;
var imgArray = <?php echo json_encode($imgArray) ?>;
var prArray = <?php echo json_encode($prArray) ?>;
</script>
<script src="includes/script.js"></script>
<body>
<script>
createTables();
addImage(1,2);
</script>
</body>
</html>
function addImage(){
document.getElementById('c'+r+''+c).innerHTML('<img style="max-height:'+screenH+';max-width:'+screenW+';" src="'+imgArray[1]+'"');
}
Tis function depends on values of r and c but those values don't seem to be passed to the function.
Do you maybe want to define it with:
function addImage(someR, and Some c){
//...
}
and then call:
addImage(1, 2)
Related
I have the following code which returns the result I require inside of a tab. But because the JavaScript is in the same file it shows a blank tab when there is no data to show. I remove the JavaScript and the tab disappears. How can I run the JavaScript only if data is present so the tab will disappear? Or can I call it from another file?
<?php echo $block->escapeHtml($block->getProduct()->getData($this->getCode()));
?>
<script type="text/JavaScript">
var commareplace = document.querySelectorAll("div > #bikefitment");
for (var i = 0; i < commareplace.length; i++) {
commareplace[i].innerHTML = commareplace[i].innerHTML.replace(/,/g, "<br />");
}
</script>
Since you have to hide the tab if escapeHtml() returns empty value. So you can create a condition block to add the script if escapeHtml() returns a non-empty value.
<?php $EH = $block->escapeHtml($block->getProduct()->getData($this->getCode()));
if ($EH) {
echo $EH;
?>
<script type="text/JavaScript">
var commareplace = document.querySelectorAll("div > #bikefitment");
for (var i = 0; i < commareplace.length; i++) {
commareplace[i].innerHTML = commareplace[i].innerHTML.replace(/,/g, "<br />");
}
</script>
<?php } ?>
My knowledge on PHP is not the best, so I will just describe it like that.
Just save the data in a variable
If the data exists (e.g. is not empty)
Print data and JavaScript code
If it does not exist, do nothing
Here is some pseudo-code:
<?php
data = $block->escapeHtml($block->getProduct()->getData($this->getCode()));
if (data exists) {
echo data;
echo /* Your JavaScript Code*/;
}
?>
Use a if statement that within the php tags that check value id present then you can render the the java script tags as well as the value.
<?php
$val = $block->escapeHtml($block->getProduct()->getData($this->getCode()));
$script = '<script type="text/JavaScript">
var commareplace = document.querySelectorAll("div > #bikefitment");
for (var i = 0; i < commareplace.length; i++) {
commareplace[i].innerHTML = commareplace[i].innerHTML.replace(/,/g, "<br />");
}
</script>';
if($val != "" || $val !=null)
{
echo $val;
echo $script;
}
?>
I have an object array var codropsEvents={[date1]:['event1'],[date2]:['event2'};
I want to insert multiple values to event1 i.e {[date1]:['event1','event2'],..}
I use the following code
<?php
$da=[];
$qry="select * from events";
$ex=mysqli_query($con,$qry);
while($row=mysqli_fetch_assoc($ex))
{
$timestamp = strtotime($row['date']);
$date= date('m-d-Y', $timestamp);
$event=$row['event'];
$da[]=$date;
$eve[]=$event;
}
?>
<script type="text/javascript">
var a=<?php echo(json_encode($da)); ?>;
var ev= <?php echo(json_encode($eve)); ?>;
var codropsEvents ={};
for(var i=0;i<a.length;i++)
{
codropsEvents[a[i]] = '<span>'+[ev[i]]+'</span>';
}
</script>
But using this code I got something like this,
var codropsEvents={[date1]:[event1],[date1]:[event2]}
But I need all events with same date in a single key is {[date1]:['event1','event2',..],[date2]:[''event3','event4'..],..};
Please anyone can help me
You need to check whether the key exists in the object, if not initialize it with empty array. Then push elements into the array.
<script type="text/javascript">
var a=<?php echo(json_encode($da)); ?>;
var ev= <?php echo(json_encode($eve)); ?>;
var codropsEvents ={};
for(var i=0;i<a.length;i++)
{
if (!codropsEvents[a[i]]) {
codropsEvents[a[i]] = [];
}
codropsEvents[a[i]].push('<span>'+[ev[i]]+'</span>');
}
</script>
I'm trying to display a PDF file that I stored in my database as a BLOB file. Although I have tried a couple of ways I still can't seem to get it right.
Here is how I (try to) download a PDF file from my database:
function fillArrays(){
$idArray = array();
$sql = "SELECT oglas_id,slika,prioriteta FROM deska WHERE deska.datumz <= CURRENT_DATE AND deska.datumk >= CURRENT_DATE;";
$result = mysqli_query($GLOBALS['conn'],$sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck>0){
while($row = mysqli_fetch_assoc($result)){
array_push($GLOBALS['idArray'],$row['oglas_id']);
$b64Doc = chunk_split(base64_encode(file_get_contents($row['slika']))); // THIS LINE!
$file = "data:application/pdf;base64,".$b64Doc; // THIS LINE!
array_push($GLOBALS['imgArray'],$file); // THIS LINE!
array_push($GLOBALS['prArray'],$row['prioriteta']);
}
}else{
die("Oops there seems to be an error(arr)");
}
}
The code I've marked "THIS LINE!" should put all my PDF files from the database into an array of PDF files called $imgArray. Then I json_encode the array and assign it to the imgArray variable in JavaScript:
var imgArray = <?php echo json_encode($imgArray) ?>;
And finally, I try to display the same PDF file 4 times in a table created via JavaScript. (I call createTable() in main PHP file):
var idArray = window.idArray;
var imgArray = window.imgArray;
var prArray = window.prArray;
var screenH = screen.height/2.1;
var screenW = screen.width/2.1;
var tdW;
var tdH;
function createTables(){
if(idArray.length>2){
rows=2;
cols=2;
tdW='100%';
tdH='100%';
}else if(idArray.length==2){
rows=1;
cols=2;
tdW='100%';
tdH='100%';
}else if(idArray.length==1){
tdW='100%';
tdH='100%';
}
var table = '';
for(var r=0;r<rows;r++){
table += '<tr>';
for(var c=0;c<cols;c++){
table += '<td style="max-width:'+tdW+';max-height:'+tdH+';" id="c'+c+r+'">';
table += '<embed src="'+imgArray[1]+'" type="application/pdf" style="max-height:'+screenH+';max-width:'+screenW+';">';
table += '</td>';
}
table += '</tr>';
}
document.write("<table>"+table+"</table>");
}
See at the bottom of the code where I try to embed the pdf file. Output on the site is this:
The language is Slovene and it says that the PDF file is corrupted.
You json_encode the data server-side but then don't call JSON.parse on the client-side. This means you are outputting JSON-encoded data directly into the <script src=""> attribute.
Change the PHP encoding process to remove unnecessary function calls:
$b64Doc = base64_encode($row['slika']);
Then you'll need to modify your JavaScript to correctly parse the JSON. Something like:
var imgArray = JSON.parse('<?= json_encode($imgArray); ?>');
for (var i = 0; i < imgArray.length; i++) {
table += '<embed src="' + imgArray[i] + '"></embed>';
}
I was making an image gallery with up and down buttons ,the array $imageids contains all image ids. I want to access these values with
JavaScript as follows,
PHP :
require"./connect.php";
$imgquery = mysql_query("SELECT * FROM press INNER JOIN subscriptions ON press.userid=subscriptions.to_u WHERE subscriptions.from_u='30' ORDER BY press.id DESC ");
$countrow = mysql_num_rows($imgquery);
$imageids = array();
while($fimgq = mysql_fetch_assoc($imgq)) {
$imgid = $fimgq['id'];
array_push($imageids,$imgid);
}
JavaScript:
$(document).ready(function() {
var i = -1;
var cr = <?php echo $countrow ?>;
$('#down').click(function() {
if (i < cr-1) {
i = i + 1;
var ard = <?php echo $imageids[i]?>;
alert(ard);
}
});
$('#up').click(function() {
if(i>0) {
i = i - 1;
var aru = <?php echo $imageids[i]?>;
alert(aru);
}
});
});
I want to get $imageids elements by putting a javascript variable i inside $imageids[] array , like
var ard = <?php echo $imageids[i]?>;
alert(ard); // Which doesn't work
As #chris85 and #Khalos pointed out in comments, the PHP variables are all dead once the JavaScript runs, so you need to output all the ID:s into an JavaScript array. It would looke something like this:
$(document).ready(function(){
var i=-1;
var cr= <?php echo $countrow ?>;
//Save all the ID:s to a JS variable so they can be used later.
var imageids = [<?php echo implode(',', $imageids); ?>];
$('#down').click(function(){
if(i<cr-1) {
i=i+1;
alert(imageids[i]); //This uses the JS variable imageids, not the dead PHP one.
}
});
$('#up').click(function(){
if(i>0) {
i=i-1;
alert(imageids[i]); //Same as above.
}
});
});
i have this JS:
$.getJSON('getMessageDetails.php', function (json) {
var uc = json[4];
var uc_length = uc.length;
var firstPartLength = uc_length - 5;
var uc_1 = uc.substring(0, firstPartLength);
var uc_2 = uc.substr(-5, 3);
var uc_3 = 'RC';
if (json[2] != old_id)
{
$("#td_id").text(json[2]);
$("#td_id_new").text(json[2]);
$("#td_subject").text(json[3]);
$("#span_1").text(uc_1);
$("#span_2").text(uc_2);
$("#span_3").text(uc_3);
old_id = json[2];
}
});
then i have my php/html
if (count($row) > 0)
{
echo "<input type='hidden' id='td_id_new' value='".$new_id."'></input>";
echo $new_id;
echo "<tr>";
echo '<td><a class="red_link" href="'.ADDRESS.'view_message.php?id='.$new_id.'"><span class="red_link" id="td_subject">'.$subject.'</span></a></td>';
echo '<td><span id="span_1">'.$uniqueCode1.'</span><span id="span_2" class="pink_text">'.$uniqueCode2.'</span><span id="span_3">'.$uniqueCode3.'</span></td>';
echo "</tr>";
}
all i want is to set $new_id via my json $("#td_id_new").text(json[2]);
when i inspect element the output there gives $new_id as 0....as i initialized it and not with the new id everything else(all other tags) updates fine with my json
inspect element:
<input xmlns="http://www.w3.org/1999/xhtml" type="hidden" id="td_id_new" value="0">7</input>
i got the value property of the above to be 7 thus $new_id is 7 BUT STILL nothing in the a tag for $new_id???
some help please?
thank you
you set new_id=0 in your PHP.
If you take that out and have initialized the JSON like you said, it should work.