Exploding an Echo that is separated by commas into new lines - php

I'm echoing the results a multi-select dropdown, but they appear in one line, with commas and no spaces. Users fill out a form, and select as many items as they want from the dropdown. On their profile, I want to display their selections.
Market Segment:<br>
,Health Systems-Large,Health Systems-Small/Medium,Community Hospitals
I want them to display as:
Market Segment:<Br>
Health Systems-Large<br>
Health Systems-Small/Medium<br>
Community Hospitals
My current code:
<?php if ($user["market_segment"] !="") { ?>
Market Segment:<br><?php echo $user["market_segment"];?>
<?php } ?>
EDIT - Solution:
echo str_replace(",", "<br />", $user["market_segment"]);

Like this:
echo 'Market Segment:<br>';
foreach (explode(',', $user["market_segment"]) as $market_segment) {
echo $market_segment . '<br>';
}

You want to subsitute the , with a new line. So str_replace is your friend here.
echo str_replace(",", "<br />", $user["market_segment"]);
(That is if you are serving this information on a homepage, which it seems you are doing. If not you would wnat a \n instead of br tag)

Related

Delete two first words and first words in a string (title wordpress)

currently I use tags in my wordpress article titles and place them at the beginning.
https://alerte-prolo.fr/les-alertes-alimentaires/
https://alerte-prolo.fr/alimentaire/acheter-ses-fruits-et-legumes-moins-chers/
I want to keep the tags in the category pages, but in the articles (single.php) I want to delete the tags.
currently I use this to lift the first word of the title.
<h2>
$originalTitle = the_title('','',false);
echo substr($originalTitle,strpos($originalTitle, ' '));
</h2>
t works with [Astuce], [gratuit] ect
but when I have tag [bon plan] it would be necessary to be able to delete the first two words.
I need help for this.
thanks
I think this working for you:
<?php
$originalTitle = "[bon plan] Some Title"; // In your case: $originalTitle = the_title('','',false);
$newTitle = explode( "] " , $originalTitle, 2);
echo $newTitle[1];
?>
If I understood You correctly:
$originalTitle = "[wtf] [this one to] Some Title";
$newOriginalTitle = preg_replace("/\[[^)]+\]/","",$originalTitle);
echo $newOriginalTitle;
/\[ - defining start
\] - defining end

How to display a list of check box items one per line

I have a list of items that a user can select from when listing their house. I then display these on the frontend using the following:
<? if($group[property_amenities] != "") { ?>
<hr>
<h2 class="vmargin">Amenities</h2>
<?php echo $group[property_amenities]; ?>
<? } ?>
The issues is that I believe I don't have access to change the HTML and the items are listed as follows:
Amenities
Open Plan,Carpeted Floors,
How can I modify the above code to display the items in separate rows, as well as remove the "," or replace the "," with something like " - "
Use str_replace to replace all the commas with break tags, or dashes, or whatever you want.
<?php echo str_replace(',', '<br>', $group[property_amenities];) ?>

Issue echoing links PHP MySQL, missing code

I am having trouble with my code. I want to display posts on one page grouped by category but I also need the titles of the posts to be links to the actual articles, everything I’ve tried so far has broken. Can anyone help? I can echo the titles just not as links!
You should take the excellent advice from #Marten and #CharlesAddis in the comments, and if you don't want to use an ORM, you should at least use PDO.
However, to give an answer for answer's sake:
while ($row = mysqli_fetch_array($result)){
echo "<h2>".$row['category']."</h2>"."<br />";
$titles = explode(", ",$row['titles']);
foreach ($title as $t) {
echo '' . $t . '<br>';
}
echo "<hr />";
}
I'm not sure what the relation between post titles and post links are, if you can add that I can update the answer.

Stopping php returning the same result multiple times

I am trying to display a list of friends using PHP and SQL, and my code partially works. However, it is returning the same result on multiple occasions and I would like it not to.
The SQL:
$sql =
"SELECT ubuser.usr_firstname, ubuser.usr_lastname, ubuser.usr_DOB,
ubuser2_1.usr_firstname & \" \" & ubuser2_1.usr_lastname
AS UBFriend, ubFriendsLink.ub_lnkID1, ubFriendsLink.ub_lnkID2, ubuser.usr_ID,
ubuser2_1.usr_ID
FROM ubuser
AS ubuser2_1
INNER JOIN (ubFriendsLink INNER JOIN ubuser ON ubFriendsLink.ub_lnkID1 = ubuser.usr_ID)
ON ubuser2_1.usr_ID = ubFriendsLink.ub_lnkID2
WHERE (((ubFriendsLink.ub_lnkID1) = ".$_SESSION['usr_ID'] ."))
OR (((ubFriendsLink.ub_lnkID2) = ".$_SESSION['usr_ID'] ."))";
The SQL works (or seems to).
The code for displaying the result:
<?php
$nrecs=0;
while (!$FriendsRs->EOF) {
$nrecs++;
?>
<? php
if (.$SESSION['usr_ID'] == ['ub_lnkID1'])
{
echo <p>Name: <?php echo $FriendsRs->Fields['UBFriend']->Value ?><br/ >
<? php
else
echo <p>Name: <?php echo $FriendsRs->Fields['usr_firstname']->Value ?> <?php
echo $FriendsRs->Fields['usr_lastname']->Value ?><br />
<?php $FriendsRs->MoveNext() ?>
<?php } ?>
The result:
Name: Carl Smith
Name: Rob Sanderson
Name: Rob Sanderson
Name: Tony Jackson
The problem seems to be that what I am getting is a list of the names associated with the ub_lnkIDs, where i only want to display the individual names. (FYI, Rob Sanderson is not needed, but it can be returned once).
EDIT:
The desired output is
Name: Carl Smith
Name: Tony Jackson
From what you've described it sounds like you want to remove the duplicates from your list.
I'm not sure what you're trying to do with the session check but your original if/else condition didn't make sense so I changed it to what might be right based on your query.
<?php
$Arrnames = array();
// Produces array containing all the names in the correct format
while (!$FriendsRs->EOF)
{
if ($SESSION['usr_ID'] == $FriendsRs->Fields['ub_lnkID1'])
{
array_push($Arrnames, "<p>Name: ".$FriendsRs->Fields['UBFriend']->Value."<br />");
}
else
{
array_push($Arrnames, "<p>Name: ".$FriendsRs->Fields['usr_firstname']->Value ." ". $FriendsRs->Fields['usr_lastname']->Value . "<br />");
}
$FriendsRs->MoveNext();
}
// Removes the duplicates from the array
$ArrnamesDedupped = array_unique($Arrnames);
// Loops the de-duplicated array and echos the result
foreach ($ArrnamesDedupped as $value)
{
echo $value;
}
?>
if (.$SESSION['usr_ID'] == ['ub_lnkID1'])
My best guess is the single = is causing your if statement to fail
== means is the same as (and can return false) WHEREAS = will always return true! (= is used for setting values.)
Additionally you open <?php tags above the if statement - and then open them again in your <?php echo without closing them?
<? php
if (.$SESSION['usr_ID'] = ['ub_lnkID1'])
{
echo <p>Name: <?php echo //HERE
There are multiple problems.e.g. opening
php tags inside php tags, no closing } for the if clause, html paragraphs (p) opened and not closed etc.
what I think you want to do (not tested):
<?php
$nrecs = 0;
while (!$FriendsRs->EOF) {
$nrecs++;
if ($SESSION['usr_ID'] !='ub_lnkID1') {
echo "<p>Name:" . $FriendsRs->Fields['usr_firstname']->Value . " " . $FriendsRs->Fields['usr_lastname']->Value . "</p>";
}
$FriendsRs->MoveNext();
}
?>

How to display the result as a vertical list for this code?

My code is as shown.But the results are displayed as a horizontal list like this :
Questions selected
Explain how to produce the vector image, List THREE of image file types or formats used in multimedia, construct a diagram
$a= array(implode(", ", $_GET["choice"]));
echo "<table align=\"center\">";
echo "<tr><th>Questions selected </th></tr>";
while (list ($key, $val) = each ($a)) {
echo"<tr><td>";
echo " $val ";
echo "</td></tr>";
}
echo "</table>";
I want them to be displayed like this :
Questions selected :
Explain how to produce the vector image
List THREE of image file types or formats used in multimedia
So how can i list them 1 by 1 vertically ?Can somebody help me please ?I have tried putting them in table but i'm still getting them display horizontally.
You build $a as an array containing only one string. This is why it shows only one line.
implode takes and array, and join each item. If you want to separate a string into an array, you need to use explode.
Try with simply:
$a = $_GET["choice"];

Categories