passing variables in query string - php

$courses_taken=$row['course_id'];
As you can see from above code, i'm taking a variable and passing it in query string, but this method is wrong.
How can i pass a variable in query string, as each user will have opted for different courses, thus they will have different course_id.
Is there a way to do it?

<?php
$courses_taken=$row['course_id'];
?>

You need an echo statement, and then interpolate the variable inside the string.
$courses_taken = $row['course_id'];
echo "<a href='course.php?course_id=$courses_taken'>click here</a>";

Try this:
You are not using php tag to pass php variable in your anchor tag.
Do it like this:
-
Thanks

Related

What is the correct syntax in php in passing variable(s) in a HTML link to another form

This is what I am trying to do. I am trying pass variable(s) from one form to another. I can do it in "HTML" I would like to have done in a PHP echo statement
When I call the second program from the first program I have nothing coming back in the "GET" array. I think ($v_resource_id, $v_category_id) are not being translated correctly What I am doing wrong in my syntax ? What you see here is part of the first program or calling program
echo'';
Aside from the noted html error (missing quote), you are not retrieving the correct keys, you have var and var2 but are trying to retrieve v_resource_id. You need to retrieve $_GET['var'] and $_GET['var2'].
Do print_r($_GET); to see what is being sent via $_GET. You should get:
Array (
[var] => whatever
[var2] => something else
)
Also, you may want to use the native query building function by doing http_build_query(array('var'=>'something','var2'=>'what ever')) to get a query string. Makes this easier and I think it takes care of urlencode(), you'd have to double check that.
Try this, it's about proper quoting
echo "here";
you'll use your values as $_GET['var'] and $_GET['var2']

How to store a echo result into a variable in php?

I am a new bee to this. I am trying to store the result of a random value generated into a variable which then I will be storing in MySQL DB.
Tried a code like below
$des_nu1 = echo rand(100,9999);
but looks like the statement isn't correct. Please advise.
I did see an option to use session but is there any alternative than using session ( Ref using Session : How to store a variable in php using session)
Remove the echo
$des_nu1 = rand(100,9999);
The only thing echo does is this Output one or more strings. Since you don't want to output the result of your function you don't need echo. After you assign that value to your variable you can use it anyway you want and you can also use it for echo
echo $des_nu1;
$temp = $des_nu1 + 10; //etc
Always refer to the manual,
echo is language construct echo manual that returns nothing, it will write in standard output (stdout)
I advise you to always use sprintf()
sprintf("%i", rand(100,9999));

PHP: How to add a variable inside other variable name?

How I can add a variable inside other variable definition?
Here is an example: $cars_list_ VARIABLE HERE [$car]
${'cars_list_'.$your_variable}[$var]
But it is extremely ugly. Use an array instead of multiple variables:
$vars_list[$your_variable][$car]
Obviously you need to change your existing code for that to work. But it's worth the time/effort.
While you could use ${'cars_list_'.$somevar}['car'], it is an extremely bad idea. Instead, you should have a multi-dimensional array that you can access with $cars_list[$somevar]['car']
You can do:
${'cars_list_' . 'other_var'} = "stuff";
echo $car_list_other_var;
Returns "stuff"

PHP variable in a HTML string created from Javascript

I'm trying to use some simple AJAX and then passing my variable through a HTML string.
Struggling to get some PHP included to print the value into the string. Been trying like this and the variable prints blank even though there is a value assigned to $user_id.
xmlhttp.open("GET","update.php?user=<?php $user_id ?>&q="+str,true);
Any suggestions on how I could do this or better solutions.
Thanks
you forgot echo:
xmlhttp.open("GET","update.php?user=<?php echo $user_id; ?>&q="+str,true)

some quick help with this small piece of code

How come when I echo $p, the variable which Im trying to fetch using this loop doesnt get displayed in the path.
$name_image2="picture.jpg";
for ($i=2; $i<=$nr_of_pics; $i++){
$img='name_image'.$i;
echo $$img; gives me this: 'picture.jpg' which is correct.
but when echoing $p like this:
$p="/SV/main/temp_images/$$img"; echo $p;
I get this: SV/main/temp_images/name_image2 --> the variable 'name_image2' doesnt get called here, why?
I want it to say: SV/main/temp_images/picture.jpg
Thanks
$p = "/SV/main/temp_images/" . $$img;
Ought to fix it.
Also, I would recommend learning how to use arrays. They are a much better way to have a set of data instead of variable variables.
Try $p="/SV/main/temp_images/{${$img}}";
When PHP is parsing the string and comes to a $, it looks at the next character to see if it makes a valid variable name. If not, it moves on. In this case, that means that the second $ is correctly interpreted, but the first one has already been passed by. The answer is to enclose the inner expression in brackets, so that it will be parsed before the outer one is.

Categories