I have a map on my site that has plots placed on it using data from my table. The plots are placed with javascript as so...
addPostCode('<?php echo $array[$UID][0]; ?>');
addPostCode('<?php echo $array[$UID][1]; ?>');
addPostCode('<?php echo $array[$UID][2]; ?>');
Somehow I need to see if the variable exists in my table, and then if so, generate an additional line and increment my array number, so based on the above 3 records, the next line would be...
addPostCode('<?php echo $array[$UID][4]; ?>');
Can I ask if this is possible?
You just need to loop through all of the members of the $array[$UID] variable.
<?php
foreach($array[$UID] as $thing) {
$encodedThing = json_encode($thing);
echo "addPostCode($encodedThing);\n"
}
?>
I used json_encode() because it does whatever escaping is necessary to make your data JavaScript-compatible.
If I understand you well, this is what you would do:
addPostCode('<?php $i=0; while (isset( $array[$UID][$i])) { echo $array[$UID][$i]; $i++ ; } ?>');
That would replace all your addPostCode() calls also.
Related
hi guys im trying to show and hide div according to mysql value but i couldnt do it can you help me what im doing wrong
here is my code thanks a lot for your ideas
var Value = <?php echo json_encode($valuek) ?>;
if (Value==1){
$('#show_hide').show();
}
else{
$('#show_hide').hide();
}
<?php
$valuek = $session->userinfo['vcc'];
?>
<div id="show_hide">
some code
</div>
<?php echo json_encode($valuek) ?>
will return a json string, instead try just using "echo"
<?php echo $valuek ?>
If all you are going for is a boolean value then there is simply no need for JSON.
Echo the value directly into the JavaScript. Remember to ensure you are passing a valid boolean value.
PHP code -
<?php
$showDiv = ($dbValue == 1? 'true' : 'false');
?>
JavaScript + PHP injection -
<script>
var value = '<?php echo $showDiv; ?>';
<script>
Don't forget to wrap the PHP injected value with quotes.
$valuek = $session->userinfo['vcc'];
I'm not sure if you have the code in this order in your php file, or just showed pieces of code in this order, but Should go BEFORE your js code. It has no value when js code is run.
To see what $valuek is, just echo it on top of the screen
<?php echo "<h1>$valuek</h1>" ?>.
Or just look at the source - at your js function, to see what is printed after 'var Value ='
That's the main thing really - to make sure that you getting what you expect from session.
And as been said, you don't need jason_encode, but you do need a semi-colon after echo command.
Also, I hope your jquery code is within $(document).ready function, not as is.
My included file (include.php) is this:
<?php
$myarray=(a,b,c);
shuffle($myarray);
?>
My main php file is this:
include('include.php');
if isset($_POST['submit_button']){
echo "Button was clicked";
echo $myarray;
}
else {
echo "Not clicked.";
echo $myarray;
}
?>
<form method='POST'><input type='submit' name='submit_button'></form>
Why are the elements of $myarray displayed in a different order after I clicked the button? Isn't it shuffled only once?
How can I prevent the shuffle from being executed more than one time? (so that I can display the elements of myarray in the same order, before and after the button was clicked)
Your PHP files are interpreted upon every request. As you have it now, there is no memory in your system, so there's no way for your files to "remember" that the array has already been shuffled. Furthermore, if you shuffle the array once, and then load the page a second time, and managed not to shuffle it, the array would be (a,b,c), as the variable is initialized to (a,b,c) and never shuffled.
To do what you want, if I understand it correctly, you could use sessions.
$myarray=(a,b,c);
if (!isset($_SESSION['shuffled'])) {
shuffle($myarray);
$_SESSION['shuffled'] = $myarray;
} else {
$myarray = $_SESSION['shuffled'];
}
This is happening because each time you load the page, the file is being included which also shuffles the array again.
Try using serialize() and then POST the array in the order you want. Retrieve it using unserialize()
http://www.php.net/manual/en/function.serialize.php
I have a personal message system in my website done simply with php/sql. Actually I am facing the trouble to display them using jquery. The db has as fields: message_id, message_from, message_to, message_topic, message_subject and message_status. The way I am showing the message_topic is repeating eight times the following:
echo '<table><tr><td>';
retrieve_msg_topic($result);
echo '</td></tr>'; //of course I won't make 8 tables!!!
the function called is:
function retrieve_msg_topic($result)
{
if($row = mysql_fetch_assoc($result))
{
echo $row['usernombre'];
$message_topic = stripslashes($row['message_topic']);
echo '<div id="msg'.$row['message_id'].'">';
echo $message_topic;
echo '</div>';
//this will return: <div id="msgN">message topic (title, commonly subject)</div>
}
} //end function retrieve msg topic
So far I have a list on a table with the last eight messages sent to the user. The following row is reserved for pagination (next/prior page) and, after that, another row showing the message I select from the list presented, like we see in Outlook. Here is my headache. My approach is to call another function (8 times) and have all of them hidden until I click on one of the messages, like this:
echo '<tr><td>';
retrieve_msg_content($result);
retrieve_msg_content($result); //repeat 8 times
echo '</td></tr></table>';
the function this time would be something like this:
function retrieve_msg_content($result)
{
if($row = mysql_fetch_assoc($result))
{
echo '<script type="text/javascript">
$(document).ready(function(){
$("#msg'.$row['message_id'].'").click(function(){
$(".msgs").hide(1000);
$("#'.$row['message_id'].'").show(1000);
});
});
</script>';
echo '<div class="msgs" id="'.$row['message_id'].'" style="display: none">'
.$row['message_subject'].
'</div>';
}
/* This function returns:
// <script type="text/javascript">
// $(document).ready(function(){
// $("#msgN").click(function(){
// $(".msgs").hide(1000);
// $("#N").show(1000);
// });
// });
// </script>
// <div class="msgs" id="N" style="display: none">Message subject (body of message)</div>
*/
} //end function retrieve msg content/subject
I could simply explain that the problem is that it doesn't work and it is because I do if($row = mysql_fetch_assoc($result)) twice, so for the second time it doesn't have any more values!
The other approach I had was to call both the message_topic and message_subject in the same function but I end up with a sort of accordion which is not what I want.
I hope I was clear enough.
The easiest way to fix your troubles would be to copy the results of the MySQL query into an array
while($row = mysql_fetch_assoc($result)) {
$yourArray[] = $row;
}
And then use that to build your tables.
edit: What I meant was more along the lines of this:
while($row = mysql_fetch_assoc($result)) {
$yourArray[] = $row;
}
echo '<table>';
foreach($yourArray as $i) {
retrieve_msg_topic($i);
}
echo '<tr><td>';
foreach($yourArray as $i) {
retrieve_msg_content($i);
}
echo '</tr></td></table>';
And then removing everything to do with the SQL query from those functions, like this:
function retrieve_msg_topic($result) {
echo '<tr></td>'$result['usernombre'];
echo '<div id="msg'.$result['message_id'].'">';
echo stripslashes($result['message_topic']);
echo '</div><td></tr>';
}
Right now you're doing some weird key mojo with ret[0] being the topic and $ret[1] being the message, which isn't a good practise. Also, I don't see the declaration of $i anywhere in that code.
The error suggests that the result is empty or the query is malformed. I can't be sure from the code I've seen.
A few other notes: it seems weird that you're using stripslashes() on data that's directly from the DB. Are you sure you're not escaping stuff twice when inserting content into the DB?
Always use loops instead of writing something out x times (like the 8 times you said in your question). Think of a situation where you have to change something about the function call (the name, the parameters, whatever). With loops you have to edit 1 place. Without, you need to edit 8 different places.
BTW, another solution to this problem would be using AJAX to load content into the last cell. If you're curious, I could show you how.
more edits:
For AJAX, build your message list as usual and leave the target td empty. Then, add a jQuery AJAX call:
$('MSG_LIST_ELEMENT').click(function() {
var msgId = $(this).attr('id').replace('msg','');
$.get(AJAX_URL+'?msgID='+msgId,function(data) {
$('TARGET_TD').html(data);
})
});
Replace the capitalized variables with the ones you need. As for the PHP, just echo out the contents of the message with the ID $_GET['msgID'].
However, make sure you authenticate the user before echoing out any messages, so that someone else can't read someone's messages by switching the id number. Not sure how authentication works on your site, but this can be done by using session variables.
I have a php function which displays a rating bar with the arguments. I have a variable called itemID inside my php page which holds the unique item number. I need to send this value to my function and also echo command must stay. Is there a way to achieve this?
Here is the code, which does not work. When I try it on the server, it does not show the id of item, it prints the variable name as it is.
<?php echo rating_bar('$as',5) ?>
What I get at html file:
<div id="unit_long$as">
instead of the item id in place of $as.
Single Quotes do not support variable replace,
$as = "test";
echo '$as'; //$as in your end result
echo "$as"; // test in your end result
echo $as; // test in your end result
//For proper use
echo " ".$as." "; // test in your end result
Update for newer PHP versions you should now use Template Syntax
echo "{$as}"
If I get what you are saying, this is what you are asking.
<?php echo rating_bar($itemID,5); ?>
With the limited code you are providing, thats what looks like you are asking.
I have a 2 field database (daynumber & datavalue), the data of which I am retrieving with php then creating and passing variables into jquery for use with a graph.
I am not doing this as an array because I need to apply a formula to the datavalue field. The formula can be altered by the user.
In jquery, I need to be able to do this:
cday_1 = <?php echo $day_1?>;
cday_2 = <?php echo $day_2?>;
tday_1 = (<?php echo $day_1 ?> * formula1 / formula2;
tday_2 = (<?php echo $day_2 ?> * formula1 / formula2;
The cday series and the tday series will be plotted separately on the graph.
My problem is that I could manually name each var in php, ie:
$day_1=mysql_result($result,$i,"datavalue");
but I have 30 rows and I'd prefer to do it with a loop. So far, I've got this far:
$i=0;
while ($i < $num) {
$datavalue=mysql_result($result,$i,"datavalue");
echo "\$day_";
echo $i;
echo " = ";
echo $datavalue;
echo "<br>";
$i++;
}
So there are a couple of problems I'm having with this.
The first problem is that while everything is echoing, I'm not sure how to actually create variables like this(!).
The second problem is that there are only 30 rows in the db, but 34 are actually outputting and I can't work out why.
I'm self-taught, so apologies for clumsy coding and/or stupid questions.
You could just store them in a PHP-side array:
$days = array(
'cday' => 'the cday value',
'dday' => 'the dday value',
etc...
);
and then via json_encode() translate them to a Javascript array/object automatically. This object could be sent to your page as an AJAX response, or even embedded into the page directly:
<script type="text/javascript">
var days = <?php echo json_encode($days) ?>;
</script>
after which you just access the values as you would any other array in JS.
Have ended up doing this:
$i=0;
while ($i < $num) {
${"day_$i"}=mysql_result($result,$i,"datavalue");
$i++;
}
I have a feeling this was a basic issue and I just didn't explain myself properly. I am unable to use an array as I needed to fiddle with formulas so this solution worked fine.