Jump to content

help assigning a text string to a variable


kannu

Recommended Posts

hello,

i am a newbie so please excuse if this is an obvious question but after 3 hours of research and trying different syntax, i cannot figure this out.

 

i have the following code in the body section which works perfectly, which basically generates a string text.

<td class="decsription_bg2"><b><span id="custom_carat"><?=$row_prod['total_carat_weight']?></span> Carat </td>

 

What I want to do is to store the text generated by the above code to a variable which i can pass alont to the next page.

 

what should i add after the above line, to store this in a variable and to pass it along the next page. the page is currently using "post" method.

 

any help would be really appreciated.

 

thanks

Link to comment
Share on other sites

I would suggest 3 choices either using $_GET, $_COOKIE, or $_SESSION

 

of the 3 i would go with $_SESSION

 

How your going to do this I would suggest the easiest way would be since you want to pass it on from page to page is

 

on the very top of your page above the HTML tag add

<?php session_start(); ?>

 

now where you have your code to randomly generate or generate how ever you do. Put something to the effect of:

 

<?php
if(!$_SESSION['carrot_weight']) {
/* put your generator here */
} else { 
$_SESSION['carrot_weight'] = $row_prod['total_carat_weight'];
}
?>

then in your Table you going to put

<?php echo $_SESSION['carrot_weight']; ?>

 

instead of

<?=$row_prod['total_carat_weight']?>

 

Also rule of thumb outside of your question try not to use an open tag like <? without the php next to it, some server most rather don't support that and will error your coding. So try to always do <?php for the beginning of your tags..

 

I also say this assuming your not using a form as I dont see form elements in your initial post, however this can be apply to forms as well..

Link to comment
Share on other sites

whoops forgot to mention if you want to get rid of the $_SESSION variable you can either do

unset($_SESSION['carot_weight']);
or 
session_destroy(); 

if you have session data on the site in use else where though i wouldnt suggest destroy as its reseting all sessions to null 

Link to comment
Share on other sites

hello

thank you guys for taking time to help me out, but sorry if i was not clear in my initial post. the

<td class="decsription_bg2"><b><span id="custom_carat"><?=$row_prod['total_carat_weight']?></span> Carat  <span id="custom_carat12"><?=$row_prod['total_carat_weight3']?></td>

 

the above is not the complete code, but the whole line is comprised of various variables which combine together to produce a string text, which changes based on user selection of different options.

 

i want to capture and assign the entire final string text produced by the above, and assign it to one variable. how can i do the same.

 

thanks

Link to comment
Share on other sites

If you want to pass the contents of a variable from one page to another probably the best way is to use a session variable as mentioned above.

 

Set a session variable:

$_SESSION['myvar']=$row_prod['total_carat_weight'];

 

Read the session variable:

$totalCaratWeight=$_SESSION['myvar'];

 

If you're linking from one page to another you can pass it in the URL (less secure)

<a href="nextpage.php?carat=<?=$row_prod['total_carat_weight']?>">next page</a>

 

From nextpage.php get the value with $_GET:

$totalCaratWeight=$_GET['carat'];

 

If you're passing integers I'd advise this:

$totalCaratWeight=intval($_GET['carat']);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.