In my database I have one column named name and in each row of that column I'm saving names like this /Mary/Sam/Bob/Michael/. To show the values in my page I need to separate them by breaking the line in each /. Can someone help me?
If I make one echo of my column I will get /Mary/Sam/Bob/Michael/ but I want:
Mary
Sam
Bob
Michael
$names=explode('/', $dbrow['name']);
foreach($names as $aname){
echo $aname.'<br>';
}
Use the explode function with array_filter function
$database_column_string = "/Mary/Sam/Bob/Michael/";
$names = array_filter(explode('/',trim($database_column_string )));
foreach($names as $name){
echo $name;
echo "<br/>";
}
\n may not render a new line in the web browser. So it's better to replace \ with the br tag:
str_replace ("/" , "<br>" , "/Mary/Sam/Bob/Michael/");
The explode function will fix your problem but you should be aware that the way you're using your database is violating the first normal form. Each record should have the same number of attributes.
Here is a guide to the normal forms http://www.bkent.net/Doc/simple5.htm
You should redesign your database to have a table called, for example, Names(id, name)
Related
I have two columns in my database that contain values entered from multiple select boxes. I used
$skills = join($_POST['skillSelect'],',');
$languages = join($_POST['languageSelect'],',');
to format them in the database. Now I want to display them to the user on a different page. They do display, but I want each on different lines. For example, let's say the user entered 'Java, PHP, JavaScript' as their language values. I want them to display as:
<p>Java</p>
<p>PHP</p>
<p>JavaScript</p>
However, they're just displaying as
Java, PHP, JavaScript
Here is my attempt using a foreach loop and explode:
<?php
$languages_explode = explode(PHP_EOL, $_SESSION['languages']);
foreach($languages_explode as $language) {
echo $language;
}
?>
I also tried:
echo "<p>".$language."</p>";
I thought the PHP_EOL delimiter would format it correctly, but I was wrong. How can I display each item on a new line, preferably within a paragraph tag? Thanks!
change this line
$languages_explode = explode(PHP_EOL, $_SESSION['languages']);
to
$languages_explode = explode(",", $_SESSION['languages']);
and then do
echo "<p>".$language."</p>";
try like this
your code from db is
Java, PHP, JavaScript
explode this to convert as array
$mystring = "Java, PHP, JavaScript";
$myArray = explode(',',$mystring);
//print_r($myArray);
foreach($myArray as $row)
{
echo "<p>".$row."</p><br>";
}
Okay so, first of all, I searched through the www for this question, and I found some question related to arrays but not exactly to mine.
Okay so as you may know, paypal only allows one custom variable to be $POST but I need to gather the product id AND the quantity of the item bought. So to do this I made my custom variable into something that would get the post like (25-1,12-3,13-4) it means, the user bought 3 items(separated by commas), where the first number is the product id (then the separator '-' comes in) and the second one is the quantity. (so they're all in the same row and column)
Now my problem is displaying it from the database. I need to get the product name and details that's why I need to separate the numbers from each array as a string and fetch the data from the database for the information of the product. (I'm using an older version of php anyway, 5.2, I guess.)Now the problem is:
1.) It returns the word 'Array' (literally) so it would say like ArrayArrayArray
2.) How do I explode/separate those data so I can get it because I need the product ID to fetch some other data... I tried exploding it into array, then exploding it again but doesn't work (most likely my code is wrong?)
Here is my code: (I've already connected to the database)
$data = mysql_query("SELECT * from transactions") or die(mysql_error());
/* My table tag and headers goes here */
while($info = mysql_fetch_array( $data )) {
echo "<tr>";
echo '<td>' . $info['id'] . '</td>';
echo "<td>";
$array = $info['product_id_array'];
$explode_array = explode(",", $array);
foreach($explode_array as $explode_more){
$explode_more = explode("-", $explode_array);
$prod_id = $explode_more[0];
$quantity = $explode_more[1];
print_r($prod_id); //should echo the 25 in the array (25-1), right?
print_r($quantity);
}
echo"</td>";
echo"<tr>";
}
If only paypal would allow multiple custom variables T_T Thank you guys. Forgive me if I can't express my question very well or my language is not good, as english is not my first language :), Good day!
Your variable names are mixed up. Inside the foreach-loop, you should do something like this
foreach($explode_array as $explode_more){
$explode_even_more = explode("-", $explode_more);
$prod_id = $explode_even_more[0];
$quantity = $explode_even_more[1];
print_r($prod_id); //should echo the 25 in the array (25-1), right?
print_r($quantity);
}
Note, that $explode_more is used inside the loop and $explore_array is left as is.
Separate this in multiple tables, never store non-atomic values in 1 column.
Certainly not when they have relation with another table.
Suppose you want to know the sales from a certain product in some period.
I've been trying to figure out how to split the array and add different titles for each of the separate titles on the page, for each of the different things that this displays. However the most I can manage to do is add a comma between the numbers and words.
I would like to add selling"1st variable price"second variable" etc however I don't quite know how to do anything other than to turn this very confusing looking bunch of letters:
user name and notes 01001000013972583957ecCCany amount-w378- v west
into anything other than this:
0,100,10000,1397258395,7ec,CC,any amount-w378- v west
Also, this is what it looks like in its JSON form:
{"selling":"0","quantity":"100","price":"10000","date":"1397258395","rs_name":"7ec","contact":"CC","notes":"any amount-w378- v west"}
I just want all the information that is in there to displayed like that however I'm not quite sure how to add the titles that is in the JSON data. I also don't have access to the external site to change anything.
A little background: what I am trying to achieve is a price look-up for a game on my website from an external site. I tried to use an iframe but it was terrible; I would rather just manually display it rather than showing their site from mine - their style and my style clash terribly.
$json = file_get_contents('http://forums.zybez.net/runescape-2007-prices/api/rune+axe');
$obj = json_decode($json,true);
$blah1 = implode( $obj[0]["offers"][1]);
print_r($blah1);
If you know where it is, you should be able to just grab it and show it out?
You can use a failsafe to check if it is present with is_array() and isset() functions - see php.net docs on them.
Your print_r should give you good valid info -- try to wrap it around <pre></pre> tags before for better readability or view the source - it will be easier!
<pre><?php print_r($obj) ?></pre>
This should be your starting point, and from here you will either take the first one of your items or loop through all with
foreach ($obj as $o) { //should be $objects, not $obj
//do whatever with $o, like echo $o['price']
}
Each offers row is a table with each field separated by row:
$item = json_decode(file_get_contents('http://forums.zybez.net/runescape-2007-prices/api/rune+axe'));
while ($offer = array_shift($item[0]->offers)) {
echo "<table>" . PHP_EOL;
foreach ($offer as $field => $value) {
echo "<tr><th>$field</th><td>$value</td></tr>" . PHP_EOL;
}
echo "</table>" . PHP_EOL;
}
http://codepad.org/C3PQJHqL
Tables in HTML:
http://jsfiddle.net/G5QqZ/
I am making a tagging system in which there are user ids separated by comma in database . I call them after words and then break them and get the real user names for those ids . I am using str_replace to replace those words from a status update with their profile links (like facebook tag a friend) . The problem is how to replace multiple tags within the sentence ?
function tags($final_message,$tag_id)
{
GLOBAL $con;
if ($tag_id!==0)
{
$tag_id_explode=explode(",",$tag_id);
foreach($tag_id_explode as $id)
{
$tag_friend_query=mysqli_query($con,"select f_name,l_name from users_profile where user_id='$id'");
$tag_friend_query_result=mysqli_fetch_assoc($tag_friend_query);
$fname=$tag_friend_query_result['f_name'];
$lname=$tag_friend_query_result['l_name'];
$final_name=$fname.' '.$lname;
$html="<a href='profile.php?id=".$id."'>".$final_name."</a>";
}
echo str_replace($final_name,$html,$final_message);
}
}
This only converts one user and if there are multiple it does not work . If i put str_replace inside the loop then it works but it converts at a time and returns two different output with same sentence and one user being converted in each . It should be multiple users converted in just once sentence .
do this
foreach($tag_id_explode as $id)
{
$tag_friend_query=mysqli_query($con,"select f_name,l_name from users_profile where user_id='$id'");
$tag_friend_query_result=mysqli_fetch_assoc($tag_friend_query);
$fname=$tag_friend_query_result['f_name'];
$lname=$tag_friend_query_result['l_name'];
$final_name=$fname.' '.$lname;
$html="<a href='profile.php?id=".$id."'>".$final_name."</a>";
$final_message = str_replace($final_name,$html,$final_message);
}
echo $final_message;
The problem seems here when you use $final_name in str_replace(), it only has one last user's final name. Instead put it in the loop and in $html you are collecting the link, so you may want to do it in this way:
$html .=yourlink;
Please try the string concatinator dot after the variable (.)
I have a string with values (which are suburbs) like so:
$suburbs = "Mawson, Elizabeth, Burnside, Elizbeth, Mawson";
There COULD be double ups in the suburbs that the string contains. I can't change this fact.
What I am trying to do is create an option list for a drop down menu that a user will use. I do not want to display the same suburb twice( or more for that matter).
What I have so far:
$suburbs = "Mawson, Elizabeth, Burnside, Elizbeth, Mawson";
//Explode the suburbs string delimited by a comma
$boom = explode(',', $suburbs);
foreach($boom as $b)
{
$suburbOptionList .= '<option value='.$b.'>'.$b.'</option>';
}
?>
<select> <?php
echo $suburbOptionList;
?>
</select>
I know that this will simply display all of the options but I really don't know how to display each suburb only once. Ive tried a few foreach,and if combinations but they look ugly and work just as bad.
Any help would be appreciated.
Cheers in advance!
Pass $boom through array_unique() and you'll be fine.
$bada_boom = array_unique($boom);
P.S.: This will not help if you have typos or variations in duplicates. (Elizbeth != Elizabeth).
In that case you will need to get creative.
Also, hw (in comments) made a good point about trimming whitespaces. If the suburbs come from an untrusted source and are improperly formatted, you may need to normalize them. This means trimming whitespaces and normalizing capitals:
$boom = array_walk($boom, 'trim');
$boom = array_walk($boom, 'strtolower');
$bada_boom = array_unique($boom);