So I hava a problem. On client side users insert theris data in textbox, radio in textarea. All number of input is stored in hidden type, sove php script on my server side knows how many input does it has. Sometimes there is just 20 inputs, sometimes 25 or 30, so the holl stuf is daynamic.
I have two questions:
1. How on server side dynamic generate variables and use them as $input1, $input2 and os on.
2. Let's say that I have somehow managde first problem so my second question is how to make query which sometimes uses only 20 parameters, sometimes 25 and so on. I don't wanna use arrays and tables;
I stareted php code:
for($i=1;$i<=$num; $i++){ //I get num from a hidden type
${"question".$i}="j";
if(isset($_POST["${"question".$i}"])){
${"question".$i}=$_POST[${"question".$i}];
echo question1; //this doesn't work but I want make created variables
//to use like this
}
else
{
echo "You have error with reading ".$i." question";
}
}
Change echo question1; by echo $question1; (append $ symbol before your var name)
Or in dynamic way:
echo ${"question" . $i}
Why would you like to use the variables like this?
If the input is dynamic use it like an array! -> Easier and cleaner.
There is good example how to handle a dynamic array input: Post array from html to php
Related
Hi I have a list of buttons with different id's like b_1, b_2, b_3 and so on. After that I have PHP code to interact with the database and get my results.
Then I have created a variable that receives the identity of the rows that it finds in the table. I would like to use the result to change the background color of the different buttons. It works fine if I write the literal identity "b_1" or "b_2" and so on but not in the form of a variable ($trans_boton).
I have tried in many ways and I can't find the key. Thanks for lighting.
$trans_boton = 'b_'.$data['numero'];
if($facta == $respuesta){
echo "<script>"; echo "document.getElementById('b_1').style.backgroundColor='red';"; echo "</script>";}
You just need to put $trans_boton in quotes, ie:
echo "document.getElementById('$trans_boton')" ;
I am successfully executing a query on an SQL database.
$sql = "SELECT id, scdTempC, scdRH, ..., gt1pt0um, reading_time FROM Sensor order by reading_time desc limit 96";
Then, there is this and I don't know, at all, what it is doing.
$scdTempC = json_encode(array_reverse(array_column($sensor_data, 'scdTempC')), JSON_NUMERIC_CHECK);
$scdRH = json_encode(array_reverse(array_column($sensor_data, 'scdRH')), JSON_NUMERIC_CHECK);
It returns a set of 96 values for each column selected. Those go into a HighCharts chart for presentation in a browser.
Then, there is this and I have no idea, at all, what it is called or what it is doing:
var scdTempC = <?php echo $scdTempC; ?>;
var scdRH = <?php echo $scdRH; ?>;
var bmeBP = <?php echo $bmeBP; ?>;
And those vars feed into the HighCharts code:
series:
[{
marker: { symbol: 'dot' }, showInLegend: false,
name: 'SCD41 Temp', data: scdTempC
}],
Is there any way to get to the individual values and do arithmetic on them? Specifically, adjust centigrade temperature to Fahrenheit or Barometric pressure in hPa to inches of Mercury. Yes, I could add another column and feed in °F or inHG, but that seems wasteful if I can adjust the numbers on the fly. The result would need to look like what came from SQL and, as far as I know, that is a CSV string of numeric values. This is being done in a .PHP file. I don't know PHP yet. If this is too crazy or complicated, then just say so and I will go the other way with adding another column of data. Maybe it is SQL that I need, not PHP. Sorry, a bit lost and it shows!
It seems like I would use "foreach" and loop through the original list making up a new list with the same format (CSV?) and adjusted values. Is that even close?
I am a long-time programmer having worked with at least 12 languages but very new to PHP and SQL. Just started working with it inside an existing project a week ago and needing some pointers for modifying it. I have done a lot, already, but got stuck, here. Since I am jumping into the middle of this, it is difficult to even know what to search for. Search hints would be gladly accepted. THANKS for any help!!!
Mike
These lines appear to be the most important for you
$scdTempC = json_encode(array_reverse(array_column($sensor_data, 'scdTempC')), JSON_NUMERIC_CHECK);
$scdRH = json_encode(array_reverse(array_column($sensor_data, 'scdRH')), JSON_NUMERIC_CHECK);
Since you say that the numbers are feeding through to the HighCharts configuration, the above must be working fine.
What the above is doing, is that each line is converting one column of the database query results into a JSON array that can be used in the JavaScript.
Since you say you have little experience in PHP, but you do have experience in SQL, I recommend you stick to what you know and use the SQL for the arithmetic. That way you have one less problem to work on, and you can concentrate on just getting that data to the chart.
What you're asking to do is quite basic, and beyond the scope of a single answer here - the short answer is that you really need to read or watch a PHP tutorial, sorry! But since you say you've used 12 languages, you should find it easy to pick up - PHP is very much like most other C-like languages, so you should not have a problem catching on.
A clue: when you want to know what a PHP function does, just put it after "https://php.net/" in a browser. example: https://php.net/array_column
It’s important to recognize that there are 4 languages in play here: SQL, PHP, HTML, and Javascript.
In a script using best practices, you will see PHP on the top, doing all the logic and data collection/manipulation (SQL calls). Under that, you’ll see a PHP close tag (?>) followed by HTML with embedded Javascript. PHP looping, conditionals, and variable substitution are accomplished as seen here:
<?php
// work with user input
// get sql stuff
// work with sql results
$scdTempC = json_encode(array_reverse(array_column($sensor_data, 'scdTempC')), JSON_NUMERIC_CHECK);
$scdRH = json_encode(array_reverse(array_column($sensor_data, 'scdRH')), JSON_NUMERIC_CHECK);
// send output to browser
?>
<html>
… snip …
<!-- simple looping example; there are other ways to do it -->
<ul>
<?php foreach($rows as $row) { ?>
<li><?php echo $row; ?></li>
<?php } ?>
</ul>
… snip …
<!-- now starts javascript -->
<script>
// note the javascript is not running yet; we are filling in values from PHP
// The javascript will run in the browser after PHP serves it
var scdTempC = <?php echo $scdTempC; ?>;
var scdRH = <?php echo $scdRH; ?>;
var bmeBP = <?php echo $bmeBP; ?>
// more javascript
// your solution is down here somewhere. While iterating through the array,
// make a call to the appropriate javascript converter function to translate
function toFahrenheit(celsius)
{
return celsius * 9 / 5 + 32;
}
function toCelsius(fahrenheit)
{
return (fahrenheit - 32) * 5 / 9;
}
</script>
Here’s the important part for you: the javascript is not executed on the server, it’s executed in the browser after the php has finished and sent its output.
It appears that you are passing an array to javascript, which is then creating the desired output.
The solution then would be to create a function in javascript to convert to the desired unit of measure, as javascript is creating its output.
My advice is to concentrate on the javascript and solve the problem there.
i have a wordpress plugin that sends a request using file_get_contents() to a url and in-turn receive an image and four variables which ofcoz are already processed so they are just four words. How to i break down this string (using php) so i can take turn the four words into variable again as soon as they are returned, and use them on that page, (page C. )
here is a diagram of what i want to do
http://itmastersworld.com/my_problem.jpg
I have tried placing a form to be part of the string returned but apparently there is no way of manupilating the data inside the form
<form><input id="test" name="avariable" type="hidden" value="<?php echo $xxxx; ?>" /></form>
it works but i cant find a way to use the value="" without submiting the form from the server(using stuff like
$yes = $_REQUEST['avariable']
,) the form is introduced in part B that is, and appears in part c.
Help ???? I basically need my php variable created in part B.!!
Since you already have control on the page B, you can send them with some delimiter and split them? For example lets say the output is
word1,,,word2,,,word3,,,word4
Your code can be
<?php
$vars = explode(",,,", file_get_contents('your_url');
// echo $vars[1];
// echo $vars[2];
// echo $vars[3];
// echo $vars[4];
?>
I'm trying to make a dynamic menu in my web, in which only some pages from each section will appear.
The code I wrote was:
$menulist=array();
$menulist[1]='file1%#16';
$menulist[2]='file2%#9';
$menulist[3]='file3%#19';
$menulist[4]='file4%#8';
$menulist[5]='file5%#13';
$menulist[6]='file6%#14';
$menulist[7]='file7%#10';
$menulist[8]='file8%#23';
$menulist[9]='file9%#19';
$menulist[10]='file10%#18';
$menulist[11]='file11%#12';
function actualizaciones($matriz)
{
$linea=explode("%#",$matriz);
echo '<li><a href="first_chunk_of_URL'.$linea[0].'middle_chunk_of_url'.$linea[1].'last_chunk_of_URL">'.${$linea[0]}[$linea[1]].'</li>;
}
echo '<ul>';
array_walk($menulist,'actualizaciones');
echo '</ul>';
Every $linea[0] string is the name of another array (not shown in this code) which contains the text that should be in every possible link corresponding to every key passed by $linea[1].
I must have done something wrong, because the hyperlinks work fine but there's no text showing on them.
use the simple character like below
echo '<li><a href="first_chunk_of_URL'.$linea[0].'middle_chunk_of_url'.$linea[1].'last_chunk_of_URL">'.${$linea[0]}[$linea[1]].'<li>';
and the problem in your code is
.'</li>;
^^^^^
here is the problem it should be
.'</li>';
If I'm reading the question right, you're asking how to use variable variables in PHP.
This can be done using the double-dollar syntax - ie $$linea[0]. See the PHP manual for more info: http://uk.php.net/manual/en/language.variables.variable.php
But if that is what you're doing, I would say you're not writing good code: if variable variables are involved, there's almost always a better way of doing it.
Can't really offer much better assistance here without understanding more about what you're trying to do, but it sounds like you should be using subarrays rather than separate named variables for everything.
Hope that helps.
I've recently thrown together a basic PHP webpage that lists information pulled from an MySQL table and displays it in various sorts. I'm wanting to allow the user to add a new item to the table, edit an item in the list and delete an item in the list without refreshing the page (Ajax).
This currently goes;
To add/edit an article you click on a link which prompts the popover ajax form, and fills it's contents (if editing) by performing the function setEdit(comment) as below;
<a class="popup-button" title="<?php echo $row['comment']; ?>" onclick="setEdit('<?php if($row['comment']){ echo $row['comment']; } else { echo "Enter comment here..."; } ?>');"><?php echo $row['listitem']; ?></a>
The setEdit() comment is as follows;
function setEdit(editcomment)
{
if(editcomment){ document.getElementById('help-us-comment').value=editcomment; }
}
Which is then, after submitting the ajax form, handled by the following php code;
if(isset($_POST['comment_text']))
$comment=$_POST['comment_text'];
$sql = "INSERT INTO table SET
comment='$comment'";
Problem: I'm having constant issues trying to get the database contents through 1, 2, 3 without falling over at a new line, single or double quote. I've tried endless combinations of replacing tags, htmlspecialchars and nl2br with no half successes - where it's got to the point that it's so convoluted and encoded/decoded now that I'm assuming that there is a far simpler and obvious way that I'm missing.
The main problem happens when trying to load the data into the form, typically having either the form fall over and refuse to populate at all (typically by the a link becoming broken by the data extracted i.e. single quote or new line) or the form being populated with special characters instead of plain text to edit.
I've tried to go into as much detail as possible, but if any more is needed I'm happy to provide. Also apologies if this is an obvious fix/mistake, and I'm being an idiot.
You have two problems here: storing and displaying.
To display you should look in to htmlentities that makes it safe HTML (it does all the quotes replacing, html encoding, etc. for you) so that your string to be safe to be displayed as plain text, or as inputs' values.
To store the data, you should sanitize your queries. You could use mysqli and bind parameters, or use mysql_real_escape_string to escape your input manually.
Otherwise, say hi to Bobby Tables ;)