Array Pointer Content Issue in php [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I posted a question earlier that was ill received and received some great suggestions but I was too unclear for people to help.Below is not my exact code but the premise of what I'm trying to achieve:
<?php
$ExampleArray = [1,2,3,4];
$pointer = 0;
?>
I then have some HTML content:
<!-- Main Content -->
<div class="large-8 large-centered columns" role="content">
<br></br>
<?php
if($example array[$pointer] === 1)
include 'includes/form.php';
else
include 'includes/buttons.php;
?>
</div>
<div class = "small-2 columns left">
<font color = "white">Next Item</font>
</div>
What I am trying to achieve is a way of incrementing the $pointer variable on the press of the button -> so the next item is run against the if/else check. preferably I don't want the page to refresh - but I am willing to take any suggestions.
Many Thanks in Advance

This is usually done by sending GET or POST-variables.
You can for example do it like this:
<?php
if (!isset($_GET['page']) or $_GET['page'] == 1) {
$next = 2;
include 'includes/form.php';
} else {
$next = 3;
include 'includes/buttons.php;
}
?>
And change the link to something like this:
<font color = "white">Next Item</font>

You have to increment that pointer value with and ajax call to a php function like
//in php
function incrementpointer()
{
pointer++;
}
//in javascript
$('#idofbutton').on('click',function(){
$.post( "link to that php", function( data ) {
},);//its a general script,you dont need data here
});
That $pointer must be a global variable in that php file to be incremented. Also you can think about making it static if you want.

Related

Syntax Higlighter For Site [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to make a website. But I need to make a syntax highlighting program that Categorize commands based on what they do. For example, one group would be I/O, another one would be Control commands. And you would color the text based off of what type of command it is. Please I need help.
<html>
<head>
<div class=“io”>Text goes here</div>
div.io {
color: #0A0A0A;
</head>
<!--
PREFIX = 'lang/'
SUFFIX = '.js'
-->
<body onload="sh_highlightDocument('lang/', '.js');">
<!--
CLASS = 'sh_java'
PREFIX + CLASS + SUFFIX = 'lang/' + 'sh_java' + '.js'
= 'lang/sh_java.js'
-->
<pre class="sh_java">
public class X {}
</pre>
</body>
</html>
There are a few problems with your code above:
You are writing your <div> in the <head> section, which is invalid HTML
You are using curly “ ” quotes on your <div> class declaration, which won't work
In addition to this, your question is incredibly vague. There don't appear to be any 'control commands' in your question.
Having said that, it sounds like you're trying to turn the text in the io class red, and some additional content in a control-commands class green.
This can be done with JavaScript's .getElementsByClassName() method and .style property as follows:
function change() {
document.getElementsByClassName('io')[0].style.color = 'red';
document.getElementsByClassName('control-commands')[0].style.color = 'green';
}
<div class='io'>IO</div>
<div class='control-commands'>Control Commands</div>
<br />
<button onclick="change()">Change</button>
Keep in mind that .getElementsByClassName returns a Node List, so you need to access the first index of that with [0] as above.
Hope this helps!

any time a negative number on site, color it red [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Thinking there's probably some javascript out there that achieves this, but everything I've found thus far seems to be very conditional. I'm trying to figure out something across my entire site that anytime there's a negative number like -30, it's always displayed in red. These values will be scraped data outputs via php. So anytime on my html page, if there's a displayed text value that's a negative, it's turned red, that's what I'm trying to do.
Not sure if this is enough details, but basically that php is outputting data like: -30%. I have a xhtml file referencing many php files scraping and outputting values and numbers. I'm trying to find a way to put some script into my xhtml file that says: if value on this page ANYWHERE is less than 0, color red. Etc
Anyone know of some good examples of this?
My test coding is this:
xml:
<p><font color="grey">All time high</font></p>
<?php include 'ref3.php';?>
<?php include 'alltimenasfullnumber.php';?>
<p><font color="grey">Yearly growth</font></p>
<?php include 'nasyearlygrowth.php';?>
php ref:
<?php
$doc = new DOMDocument;
// foriegn stocks
$doc->preserveWhiteSpace = false;
$doc->strictErrorChecking = false;
$doc->recover = true;
$doc->loadHTMLFile('http://www.money.cnn.com/data/markets/');
$xpath = new DOMXPath($doc);
$query = "//a[#class='world-market']";
$entries = $xpath->query($query);
foreach ($entries as $entry) {
echo trim($entry->textContent); // use `trim` to eliminate spaces
}
?>
I think the best solution is to wrap each result on your xhtml page into a distinctive tag (pre, in my example).
Here there is my code, hope this will help you:
<html>
<head>
<style type="text/css">
.redFont {
color: red;
}
</style>
<script type="text/javascript">
function redText() {
var elements = document.getElementsByTagName('pre')
for(i=0; i< elements.length; i++)
{
if(elements[i].innerHTML < 0) {
elements[i].classList.add('redFont');
};
}
}
</script>
</head>
<body>
<p>
<font color="grey">Yearly growth</font>
</p>
<pre><?php echo 30; ?></pre>
<pre><?php echo -12; ?></pre>
<pre><?php echo 15; ?></pre>
<script>redText();</script>
</body>
Bye,
Laura

Php variable in script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code:
<script>
newTextBoxDiv.after().html(<?php echo $do_shortcode('[dropdown]') ?>'<span class="wpcf7-form- control-wrap text-73'+ counter + '"> '+ '<input type="text" name="text-73'+ counter + '" value="" size="40" class="wpcf7-form-control wpcf7-text" aria-invalid="false"></span>'+'pocet<br>');
newTextBoxDiv.appendTo("#test");
</script>
After .html( I need get value from php. Could you tell me how to do it?
You have to generate proper Javascript. You're not. What you're doing is the equivalent of:
... .html(some text from php'<span....);
^---no opening quote
^---no closing quote
^--- no + to concatenate
Never EVER directly echo text from PHP to Javascript. Always use json_encode():
... .html(<?php echo json_encode($do_shortcode('[dropdown'])) ?> + '<span ...
Of course, this assumes that whatever function that $do_shortcode is pointing at will return some plain text. Adjust as needed for whatever it DOES return.
1) Make sure that your file is being evaluated as PHP - that is, its a .php file
2) Test to see if echo "aaa" works instead of echo $do_shortcode to see if PHP evaluation is working, but what isn't working is the "do short code" part. That will help you troubleshoot.
3) Use View Source to inspect the output.
Pls dont do thinks link this.
if you already have your data when you running the php scripte, why do you need javascript for rendering?
when you have to do it with javascript or you dont have your data at the point of rendering: use ajax.
Its simply confusing to render javascript with php that generates html.

How to insert <li> element on <ul> using pure javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am having an issue with javascript and i don't know how to solve it ...
Actually my code is working good with jsfiddle, but when i try to insert on my HTML page ,it simply doesnt work anymore ...
What i want to, is to add the < li> on < ul> each time i tried to hit the button named "Add" !
HTML code:
....
<td width="50%" valign="top">
<b> SUPER: </b>
<ul id="ul">
</ul>
</td>
....
<input type="submit" value="Add" onclick="add()"/>
....
JavaScript code:
<script type="text/javascript">
function add(){
var ul = document.getElementById("ul");
var li = document.createElement("li");
li.innerHTML = "LoL";
ul.appendChild(li);
}
</script>
The result with that code : it doesn't add anything on my HTML page when i try to hit the button...
Thankfully,
May be oversimplifying this, but here's a thought: Your input button is a SUBMIT, so when the onClick performs, the submit happens immediately thereafter, restoring the page to its original state. The process happens so quickly the appearance is that the code did nothing.
Just change the input type to "button", eg
<input type="button">

script to get users image from their login name [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm looking for the answer to a question.. that seems really complex.
I'm looking to get a file name from a user that logs in.
For example:
User logs in as 'admin' and the file that comes is /admin.jpg
User logs in as 'john' and the file that comes is /john.jpg
I currently have the script to get the users name, the script is:
<?php echo ''. get_current_user(); ?>
Hope someone can help.. I have looked all over
Jason
Well, if the name is get_current_user() then the image is
$image = "/" . get_current_user() . ".jpg"
You just need to include this in a img tag...
echo '<img src="' . $image . '" alt="Image" />';
You can save the images in a folder (named user_images for example) and than you can take the image of every user like this:
<img src="<?php echo 'user_images/'. get_current_user().'\.jpg'; ?>">

Categories

Resources