Using the PHP explode function with session variables [closed] - php

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm trying to figure out what I'm doing wrong when I combine the explode function with session variables. I've done a decent amount of research already on session variables AND the explode function, but I've had trouble finding help on "combining" the two.
Below is the code I'm using. You'll notice that $_SESSION['shirt_type'] is being "exploded". This variable collects two pieces of info, the shirt type and price. I want to explode $_SESSION['shirt_type'] into two chunks.
However, when I try to echo chunk[0] or [1] using the following,
echo "Shirt Type = {$_SESSION['$shirt_type_chunks[0]']}<br>";
echo "Shirt Price = {$_SESSION['$shirt_type_chunks[1]']}<br>";
My web page echos the following
Shirt Type =
Shirt Price =
I believe I have the syntax wrong and I have not been able to find any place that demonstrates the correct syntax when trying to echo an exploded session variable.
Any help that can be offered will be GREATLY appreciated. Also, I've VERY new to StackOverflow so if I've posted something incorrectly, I apologize.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<?php
session_start();
$_SESSION['shirt_type'] = $_POST['shirt_type'];
$_SESSION['shirt_type_chunks'] = explode("|", $shirt_type);
$_SESSION['shirt_size'] = $_POST['shirt_size'];
$_SESSION['shirt_size_chunks'] = explode("|", $shirt_size);
$_SESSION['shirt_qty'] = $_POST['shirt_qty'];
$_SESSION['price'] = $_SESSION['$shirt_type_chunks[1]'] + $_SESSION['$shirt_size_chunks[1]'];
?>
</head>
<body>
<p>
<?php
session_start();
echo "Shirt Type = {$_SESSION['$shirt_type_chunks[0]']}<br>";
echo "Shirt Price = {$_SESSION['$shirt_type_chunks[1]']}<br>";
echo "Shirt Up Charge = {$_SESSION['$shirt_size_chunks[1]']}<br>";
echo "Shirt Size = {$_SESSION['$shirt_type_chunks[0]']}<br>";
echo "Shirt Qty = {$_SESSION['shirt_qty']}<br>";
echo "Price = {$_SESSION['price']}<br>";
?>
</p>
</body>
</html>

You should start session before any output.
Change your code to:
<? session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
...
<title>Untitled Document</title>
<?
$_SESSION['shirt_type'] = $_POST['shirt_type'];
$_SESSION['shirt_type_chunks'] = explode("|", $shirt_type);
$_SESSION['shirt_size'] = $_POST['shirt_size'];
$_SESSION['shirt_size_chunks'] = explode("|", $shirt_size);
$_SESSION['shirt_qty'] = $_POST['shirt_qty'];
$_SESSION['price'] = $_SESSION['$shirt_type_chunks[1]'] + $_SESSION['$shirt_size_chunks[1]'];
?>
</head>
Second thing you have wrong here is that $_SESSION['shirt_type'] and $shirt_type can be the same variable if register_global = on on your system. Try to avoid using same names for variables as for global variables, such as $_POST['some_name'] and $some_name or $_SESSION['var_name'] and $var_name
And the third issue, noticed by #brenjt, you need only one call for session_start() in your script.
Fourth =) $_SESSION['$shirt_type_chunks[1]'] won't work. Using single quotes in PHP means that interpreter will not even try to find any variable in your string. Small example:
<?
$a = "Mike";
echo "Hi, my name is $a"; // will output Hi, my name is Mike
echo 'Hi, my name is $a'; // will output Hi, my name is $a
echo 'Hi, my name is '.$a; // will output Hi, my name is Mike
?>
Third way is a most efficient because in this case PHP sees single quotes and do not spend resources to analyse string. So change it to $_SESSION[$shirt_type_chunks[1]]
Here is what, as I suppose, you was heading to:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<?php
$shirtDetails = explode("|", $_POST['shirt_type']);
$_SESSION['shirt_type'] = $shirtDetails['0'];
$_SESSION['shirt_price'] = $shirtDetails['1'];
$sizeDetails = explode("|", $_POST['shirt_size']);
$_SESSION['shirt_size'] = $sizeDetails[0];
$_SESSION['shirt_up_charge'] = $sizeDetails[1];
$_SESSION['shirt_qty'] = $_POST['shirt_qty'];
$_SESSION['price'] = $_SESSION['shirt_qty'] * $_SESSION['shirt_price'] + $_SESSION['shirt_up_charge'];
?>
</head>
<body>
<p>
<?php
echo 'Shirt Type = '.$_SESSION['shirt_type'].'<br>';
echo 'Shirt Price = '.$_SESSION['shirt_price'].'<br>';
echo 'Shirt Up Charge = '.$_SESSION['shirt_up_charge'].'<br>';
echo 'Shirt Size = '.$_SESSION['shirt_size'].'<br>';
echo 'Shirt Qty = '.$_SESSION['shirt_qty'].'<br>';
echo 'Total Price = '.$_SESSION['price'].'<br>';
?>
</p>
</body>
</html>
One final question - why you not just create single input for each form property? Why you want this data to be concatenated by |, is it really necessary?

Related

PHP - Echo is not displaying correctly

https://pastebin.com/pEg8ZARP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Future Value</title>
</head>
<body>
<?php # Script 1.8 - futureValue.php.
// Set the variables:
$interest = .08;
$amount = 1000;
$years = 20;
// Calculate the value:
$value = $amount* pow( (1+$interest), $years);
// Format the total:
$value = number_format ($value, 2);
// Print the results:
echo "<p>The future value is <b>/$$value</b>.</p>";
?>
</body>
</html>
I tried pasting it here but the formatting got messed up.
Whenever I try to load the page it just says " The future value is /$$value.
"; ?> " instead of displaying "The future value is $4660.96.".
I am totally lost at what I am doing wrong.
echo "<p>The future value is <b>$".$value."</b>.</p>";
$ is a special character so you need to escape it. Or you can always just concat your string and variable to avoid this problem.

Generate div automatically by mysql_fetch_array

I would like to create a system to issue opinions on a given thing. I'm at a good point, but I can not get created for each element taken from the database a correspond div. Let me explain, for example if I have three reviews made by the three authors, I would like three divs for the reviews, and three for the authors, then automatically generated by the loop. How can i do?
You can do this.. First of all add <body> tag to your html document
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$Content = mysql_query("SELECT * FROM feedback");
while($Row = mysql_fetch_array($Content)) {
$Feedback = $Row['FeedbackUser'];
$UserNick = $Row['UserNickname'];
$UserMail = $Row['UserEmail'];
echo "<div>{$Feedback}</div>"; // Displaying feedback as div
echo "<div>{$UserNick }</div>"; // Displaying UserNick as div
echo "<div>{$UserMail }</div>"; // Displaying UserMail as div
}
?>
</body>
Hope this is what you are looking for

How to call XML data in PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
A simple program to CRUD node and node values of xml file
I tried calling the data in my XML in the below way. But it didn't work for me. Can anybody tell me how can I make it work?
<broad>
<site>
<title>My Site Name</title>
<caption>My Site Caption</caption>
<hostname>www.mydomain.com</hostname>
</site>
<broad>
My PHP file is
<?php
$xml = simplexml_load_file('settings.xml');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $xml->title; .'|'. $xml->caption; ?></title>
</head>
<body>
</body>
</html>
Why the above way is not working? is there any other way which is more easier than this?
You need to access the site node:
<?php echo (string) $xml->site->title?>
If ever in doubt, use a var_dump:
<?php echo '<pre>'; var_dump($xml); echo '</pre>'; ?>
Use the following:
<?php echo (string)$xml->site->title .'|'. (string)$xml->site->caption; ?>
Use SimpleXML debug with var_dump() and print_r()

Simple RSS encoding issue

Consider the following PHP code for getting RSS news on a site I'm developing:
<?php
$url = "http://dariknews.bg/rss.php";
$xml = simplexml_load_file($url);
$feed_title = $xml->channel->title;
$feed_description = $xml->channel->description;
$feed_link = $xml->channel->link;
$item = $xml->channel->item;
function getTheData($item){
for ($i = 0; $i < 4; $i++) {
$article_title = $item[$i]->title;
$article_description = $item[$i]->description;
$article_link = $item[$i]->link;
echo "<p><h3>". $article_title. "</h3></p><small>".$article_description."</small><p>";
}
}
?>
The data accumulated by this function should be presented in the following HTML format:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Новини от Дарик</title>
</head>
<body>
<?php getTheData($item);?>
</body>
</html>
As you see I added windows-1251(cyrillic) and utf-8 encoding but the RSS feed is unreadable if I don't change the browser encoding to utf-8. The default encoding in my case is cyrilic but I get unreadable feed. Any help making this RSS readable in cyrilic(it's from Bulgaria) will be greatly appreciated.
I've just tested your code and the Bulgarian characters displayed fine when I removed the charset=windows-1251 meta tag and just left the UTF-8 one. Want to try that and see if it works?
Also, you might want to change your <html> tag to reflect the fact that your page is in Bulgarian like this: <html xmlns="http://www.w3.org/1999/xhtml" lang="bg" xml:lang="bg">
Or maybe you need to force the web server to send the content as UTF-8 by sending a Content-Type header:
<?php
header("Content-Type: text/html; charset=UTF-8");
?>
Just be sure to include this before ANY other content (even whitespace) is sent to the browser. If you don't you'll get the PHP "headers already sent" error.
Maybe you should take a look at htmlentities.
This can convert to html some characters.
$titleEncoded = htmlentities($article_title,ENT_XHTML,cp1251);

How to Count number of lines of javascript in php files?

I need to count the no. of lines of inline java script between script tags in php files. How do I do it? Will grep linux command suffice or I can get some tool to do it? Please help.
You might use a regular expression like to extract the content of each SCRIPT tag in your files and than count the \n occurrences within the content.
This regex should match all script tag including the opening and closing tag:
/<script[^>]*?>(.*)?</script>/sm
You should than remove the tags and lines without any code to count the real lines of JavaScript code.
Please take a look on the following code,it works but you may need to updates as per your requirements
<?php
$file = file('thisfile.php');
for($i=0;$i<count($file);$i++)
{
if(trim($file[$i]) == "<script language=\"javascript\">")
{
$start = $i.'<br>';
}
if(trim($file[$i]) == "</script>")
{
$end = $i.'<br>';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
var a = 10;
var b = 10;
var c = 10;
var d = 10;
var e = 10;
var e = 10;
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php echo ($end - $start)-1; ?>
</body>
</html>
save this php file as thisfile.php then try
Have a nice day
If you need to do this from a processed HTML page, use DOM to get all script tags without a src attribute. Then for each found node split the child TextNode by the linebreak or simple count them. Done.
If you need to grab this from actual PHP source code, use the Tokenizer to find T_STRINGS and similar parser tokens and analyze them for <script> blocks, but note that it might be impossible to find them all, if there is code like:
echo '<' . $scriptTag . '>' . $code . '</' . $scriptTag . '>';
because that wont be analyzable as a JavaScript String before PHP processed it.

Categories