So basically I have this function called Reveal (in ubio.php) it basically allows inputted information to be revealed that has <r></r> around it when it gets shown to the end user. However - when it does this, it stores the data in an array.
The problem I come across is that when I display the page I use an echo and I'm uncertain of how to get the array to display properly. Maybe as a string... but I'm not certain.
The following is the codes I'm using.
mybio.php - this basically calls the other two php files to display their contents, ebio and vbio
<?
include('../USS_Ethos/SSI.php');
include('../USS_Ethos/BioTools/account.php');
$user_id = $context['user']['id'];
$npc = "0";
$table_bio = "bio";
$connection = @mysql_connect($db_host, $db_name, $db_pw) or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$sql_bio = "SELECT * FROM $table_bio WHERE user = $user_id";
$result_bio = @mysql_query($sql_bio, $connection) or die(mysql_error());
$bio = mysql_fetch_array($result_bio);
//global $user_id;
//global $npc;
include('../USS_Ethos/BioTools/vbio.php');
include('../USS_Ethos/BioTools/ebio.php');
?>
vbio.php - displays the end result to the user
<head><style type="text/css">
#main {width:800px; color:#000000}
#header {width:800px; height:83px; float:left; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/header.gif); margin:0 0 0 0; padding:0 0 0 0; background-color:#000000;}
#user {width:200px; float:left; position:relative; left:300px; top:28px; font-size:x-large;}
#job1 {width:200px; float:left; position:relative; left:0px; top:0px;}
#pic {width:240px; height:250px; float:left; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/ava.jpg); background-color:#000000; margin:0 0 0 0; padding:0 0 0 0;}
#img.x {z-index:-1;}
#service {width:560px; height:250px; float:right; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/service.gif); background-color:#000000; margin:0 0 0 0; padding:0 0 0 0;}
#scontent {width:470px; height:120px; float:left; position:relative; left:50px; top:90px; font-size:.78em; overflow-y:scroll;}
#vitals {width:240px; height:250px; float:left; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/vitals.gif); background-color:#000000; margin:0 0 0 0; padding:0 0 0 0;}
#vcontent {width:147px; height:120px; float:left; position:relative; left:50px; top:90px; font-size:.83em;}
#medical {width:560px; height:250px; float:right; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/medhistory.gif); background-color:#000000; margin:0 0 0 0; padding:0 0 0 0;}
#mcontent {width:470px; height:120px; float:left; position:relative; left:50px; top:90px; font-size:.78em; overflow-y:scroll;}
#awards {width:240px; height:250px; float:left; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/awards.gif); background-color:#000000; margin:0 0 0 0; padding:0 0 0 0;}
#acontent {width:147px; height:120px; float:left; position:relative; left:50px; top:90px; font-size:.78em; overflow-y:scroll;}
#backstory {width:560px; height:250px; float:right; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/backstory.gif); background-color:#000000; margin:0 0 0 0; padding:0 0 0 0;}
#bcontent {width:470px; height:120px; float:left; position:relative; left:50px; top:90px; font-size:.78em; overflow-y:scroll;}
#skills {width:240px; height:250px; float:left; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/skills.gif); background-color:#000000; margin:0 0 0 0; padding:0 0 0 0;}
#skcontent {width:147px; height:120px; float:left; position:relative; left:50px; top:90px; font-size:.78em; overflow-y:scroll;}
#family {width:560px; height:250px; float:right; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/family.gif); background-color:#000000; margin:0 0 0 0; padding:0 0 0 0;}
#fcontent {width:470px; height:120px; float:left; position:relative; left:50px; top:90px; font-size:.78em; overflow-y:scroll;}
</style></head>
<?php
//Initialize and connect to the DB
//$table_award = "award_$user_id";
$service = nl2br($bio[service]);
$skills = nl2br($bio[skills]);
$medhistory = nl2br($bio[medhistory]);
$backstory = nl2br($bio[reveal_background]);
$family = nl2br($bio[family]);
// Display the various layers
echo "
<div id=\"main\">
<div id=\"header\">
<div id=\"user\"><b>$bio[name]</b></div>
<div id=\"job1\"><b>$bio[job1]</b></div>
</div>
<div id=\"pic\"></div>
<div id=\"service\">
<div id=\"scontent\">$service</div>
</div>
<div id=\"vitals\">
<div id=\"vcontent\"><b>Gender:</b> $bio[sex]<br><b>Species:</b> $bio[species]<br><b>Height:</b> $bio[h_ft] ft $bio[h_in] in<br><b>Weight:</b> $bio[weight] lbs<br><b>Age:</b> $bio[age] years</div>
</div>
<div id=\"medical\">
<div id=\"mcontent\">$medhistory</div>
</div>
<div id=\"awards\">
<div id=\"acontent\">$awards</div>
</div>
<div id=\"backstory\">
<div id=\"bcontent\">$backstory</div>
</div>
<div id=\"skills\">
<div id=\"skcontent\">$skills</div>
</div>
<div id=\"family\">
<div id=\"fcontent\">$family</div>
</div>
</div>";
?>
ebio.php - gives the user a form that they can input their data in to
<head><style type="text/css">
#main {width:800px;}
#edit {width:800px; height:83px; float:left; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/edit.gif);}
#name2 {width:300px; float:left; position:relative; left:200px; top:28px; font-size:large;}
#tags {width:800px; height:77px; float:left; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/tags.gif);}
#vitals2 {width:240px; height:250px; float:left; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/vitals2.gif);}
#vcontent2 {width:147px; height:120px; float:left; position:relative; left:50px; top:90px; font-size:.78em;}
#service2 {width:560px; height:250px; float:left; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/service2.gif);}
#scontent2 {width:470px; height:120px; float:left; position:relative; left:50px; top:90px; font-size:.78em; }
#skills2 {width:240px; height:250px; float:left; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/skills2.gif);}
#skcontent2 {width:200px; height:120px; float:left; position:relative; left:50px; top:90px; font-size:.78em;}
#medhistory2 {width:560px; height:250px; float:left; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/medhistory2.gif);}
#mcontent2 {width:470px; height:120px; float:left; position:relative; left:50px; top:92px; font-size:.78em;}
#backstory2 {width:800px; height:250px; float:left; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/backstory2.gif);}
#bcontent2 {width:470px; height:120px; float:left; position:relative; left:288px; top:92px; font-size:.78em;}
#family2 {width:800px; height:250px; float:left; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/family2.gif);}
#fcontent2 {width:470px; height:120px; float:left; position:relative; left:288px; top:94px; font-size:.78em;}
#description {width:800px; height:77px; float:left; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/description.gif);}
#arc2 {width:800px; height:250px; float:left; background-image:url(http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/arc.jpg);}
#acontent2 {width:470px; height:120px; float:left; position:relative; left:288px; top:94px; font-size:.78em;}
#submit {width:180px; height:70px; float:left; position:relative; left:-425px; top:15px; background-color:#000000}
</style></head>
<?
//Initialize and connect to the DB
// Display the Form
echo "
<form name=\"form1\" method=\"post\" action=\"../USS_Ethos/BioTools/ubio.php\">
<div id=\"main\">
<div id=\"edit\">
<div id=\"name2\"><b>Name: <input type=\"text\" name=\"name\" value=\"$bio[name]\"></b></div>
</div>
<div id=\"tags\"></div>
<div id=\"vitals2\">
<div id=\"vcontent2\"><b>Gender: </b><br>
<input type=\"text\" name=\"gender\" size=\"16\" value=\"$bio[sex]\"><br>
<b>Species: </b><br>
<input type=\"text\" name=\"species\" size=\"16\" value=\"$bio[species]\"><br>
<b>Age: </b><input type=\"number\" name=\"age\" maxlength=\"3\" size=\"3\" value=\"$bio[age]\"> <b>years</b><br>
<b>Height: </b></br>
<input type=\"number\" name=\"height1\" maxlength=\"1\" size=\"3\" value=\"$bio[h_ft]\"> <b>ft</b> <input type=\"number\" name=\"height2\" maxlength=\"2\" size=\"3\" value=\"$bio[h_in]\"> <b>in</b><br>
<b>Weight: </b><input type=\"number\" name=\"weight\" maxlength=\"3\" size=\"3\" value=\"$bio[weight]\"> <b>lbs</b></div>
</div>
<div id=\"service2\">
<div id=\"scontent2\"><b>Years: Ship - Position, Rank - Notes<b><br><textarea name=\"service\" cols=86 rows=7>$bio[service]</textarea></div>
</div>
<div id=\"skills2\">
<div id=\"skcontent2\"><b>List Skills and Weaknesses<b><br><textarea name=\"skills\" cols=22 rows=7>$bio[skills]</textarea></div>
</div>
<div id=\"medhistory2\">
<div id=\"mcontent2\"><textarea name=\"medhistory\" cols=86 rows=8>$bio[medhistory]</textarea></div>
</div>
<div id=\"backstory2\">
<div id=\"bcontent2\"><textarea name=\"background\" cols=86 rows=8>$bio[background]</textarea></div>
</div>
<div id=\"family2\">
<div id=\"fcontent2\"><textarea name=\"family\" cols=86 rows=8 >$bio[family]</textarea></div>
</div>
<div id=\"description\"></div>
<div id=\"arc2\">
<div id=\"acontent2\"><textarea name=\"arc\" cols=86 rows=8>$bio[arc]</textarea><input name=\"user\" type=\"hidden\" value=\"$user_id\"></div>
<div id=\"submit\"><input type=\"image\" src=\"http://www.nflcoolertalk.com/USS_Ethos/BioTools/img/submit.gif\" value=\"Submit\" alt=\"Submit\"></div>
</div>
</div>
</form>";
//echo '<pre>', print_r($context['user'], true), '</pre>';
?>
ubio.php - this is called when the form is submitted and inputs the data in to the DB
<?
include('../SSI.php');
include('../BioTools/account.php');
$table_bio = "bio";
$user_id = $_POST[user];
function reveal($string) {
if(empty($string))
return array();
$string = str_replace(array('<R>', '</R>'), array('<r>', '</r>'), $string);
if(strpos($string, '<r>') === false || strpos($string, '</r>') === false)
return array();
preg_match_all("~<r>(.*)</r>~Us", $string, $output);
return $output[1];
}
$reveal_background = reveal($_POST[background]);
if ($npc == '0'){
$personality = '';
$permission = '$user_id, 1';
}
else{
$personality = '$_POST[personality]';
$permission = '$_POST[permission]';
}
$connection = @mysql_connect($db_host, $db_name, $db_pw) or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "UPDATE $table_bio SET name = '$_POST[name]', species = '$_POST[species]', sex = '$_POST[gender]', age = '$_POST[age]', h_ft = '$_POST[height1]', h_in = '$_POST[height2]', weight = '$_POST[weight]', service = '$_POST[service]', medhistory = '$_POST[medhistory]', background = '$_POST[background]', reveal_background = '$reveal_background', skills = '$_POST[skills]', family = '$_POST[family]', arc = '$_POST[arc]', npc = '$npc' WHERE user = '$user_id'";
$sql2 = "SELECT * FROM $table_bio";
$sql3 = "INSERT INTO $table_bio (id, user, name, species, sex, age, h_ft, h_in, weight, service, medhistory, background, reveal_background, skills, reveal_skills, family, reveal_family, arc, rank, job1, job2, permission, personality, npc) VALUES (NULL, '$_POST[user]', '$_POST[name]', '$_POST[species]', '$_POST[gender]', '$_POST[age]', '$_POST[height1]', '$_POST[height2]', '$_POST[weight]', '$_POST[service]', '$_POST[medhistory]', '$_POST[background]', $reveal_background, '$_POST[skills]', '$_POST[family]', '$_POST[arc]', NULL, NULL, NULL, '$permission', '$personality', '$npc')";
$result2 = @mysql_query($sql2, $connection) or die(mysql_error());
while($row = mysql_fetch_array($result2))
{
$current = $row['user'];
if ($current == "$user_id")
{
$checker = 1;
}
}
if ($checker == "0"){
// Create the Bio Entry
$result3 = @mysql_query($sql3, $connection) or die(mysql_error());}
else {
// Modify Exisiting one
$result = @mysql_query($sql, $connection) or die(mysql_error());}
/**/
header("Location:../index.php?page=98");
?>
Not quite sure what you're asking, but it looks to me like you're not escaping your variables in the echo statement. I personally prefer to use the single quotes for echo's because then you don't have to escape the double quotes for all the HTML. If I'm not even touching on your question I apologize.
For the variables I think you need to do this:
<div id=\"user\"><b>",$bio[name],"</b></div>
To give an example of the echo I would use this:
echo '
<div id="main">
<div id="header">
<div id="user"><b>',$bio[name],'</b></div>
<div id="job1"><b>',$bio[job1],'</b></div>
</div>
<div id="pic"></div>
<div id="service">
<div id="scontent">',$service,'</div>
</div>
<div id="vitals">
<div id="vcontent"><b>Gender:</b> ',$bio[sex],'<br><b>Species:</b> ',$bio[species],'<br><b>Height:</b>',$bio[h_ft],' ft ',$bio[h_in],' in<br><b>Weight:</b> ',$bio[weight],' lbs<br><b>Age:</b> ',$bio[age],' years</div>
</div>
<div id="medical">
<div id="mcontent">',$medhistory,'</div>
</div>
<div id="awards">
<div id="acontent">',$awards,'</div>
</div>
<div id="backstory">
<div id="bcontent">',$backstory,'</div>
</div>
<div id="skills">
<div id="skcontent">',$skills,'</div>
</div>
<div id="family">
<div id="fcontent">',$family,'</div>
</div>
</div>';
Just realized how much like greek the above must read... lemme see if I can simplify my problem:
mybio.php loads up vbio and ebio, ebio is basically pretty forms.
In ebio there is a textarea where you can input data in to a variable called $background.
ubio.php takes this field and runs it through a function called reveal. Reveal basically parses through the $background string to find <r></r> tags, pulls that data out, and then puts it in to a variable called $reveal_background
This variable, $reveal_background, gets called in vbio.php to display the background. The problem is $reveal_background is an array and isn't a string at this point. So instead of displaying the data in $reveal_background, it displays 'Array'.
A very quick read looks like you need to display each element of the array by designating the number of the element in brackets after the string.
Try something like this within your reveal function:
echo $reveal_background[0];
That should display the first element; follow with 1, 2, etc. for the rest. A for-each loop could be used to display each instance, once you know you're getting the elements populated correctly...
Sure - great idea.
Except you need to display data inside of a much bigger echo statement and can't run the for loop inside of that statement.
I've been trying to figure out a way to convert an array to a string so that it can be displayed withing a large echo statement, but so far I haven't had any luck.
In that case, you should echo or build your string prior to the for-each loop, then run the loop to build the background elements into a string, echo that string at the end of the loop, and go from there.
Yes - but how? The methods I've tried have ended up not yielding anything.
Would you be able to "implode" the array?
$x = implode('+', array('a', 'b', 'c')) -> $x = 'a+b+c';
With a 'glue' string of '< br />' (instead of the '+' in the example), you end up with a single string with embedded line breaks between the 'segments'.
I like the possibilities with this - what if the # of elements in the array isn't predetermined?
It doesn't matter..it just stack every item after each other, with the "glue" placed inbetween them.Thats how i too would have converted an array to a single line - short of doing a foreach loop to output the items straight away.
Doesn't matter. WIth only a single member, the 'glue' string is NOT included. It's only there as a 'connector' between multiple array members.
You might need to check if the parameter passed in IS an array (using the aptly named is_array() function). You'd have to determine whether that's going to happen (but allowing for it is pretty safe). Somthing like:
function reveal($input) {
if(empty($input))
return array();
if (is_array($input)) {
$string = implode('???', $input); }
else {
$string = $input; }
$string = str_replace(array('<R>', '</R>'), array('<r>', '</r>'), $string);
Replacing the '? ? ?' with the character or string of your choice, of course.
The $background variable is a string when it gets passed in to the reveal function. When it comes out it ends up as an array.
With the way that the current reveal function is setup...
function reveal($string) {
if(empty($string))
return array();
$string = str_replace(array('<R>', '</R>'), array('<r>', '</r>'), $string);
if(strpos($string, '<r>') === false || strpos($string, '</r>') === false)
return array();
preg_match_all("~<r>(.*)</r>~Us", $string, $output);
return $output[1];
}
Wouldn't it make more sense to put the implode after the preg_match_all function? Basically, grab $output[1] and run it through the implode function, then return that value.
Yes, you're right. It's always one consolidated string passed in.
And it's the regular expression match which results in an array.
Except it looks like the function is SUPPOSED to return an array (or why the "return array()" statements?).
But if $output is an array result which you want to consolidate into a string to be returned, then "return implode('?/?', $output);"
That did it! Thanks a lot JA!