Jump to content

[SOLVED] is there anything like implode, but vertically?


jaydeesmalls

Recommended Posts

Hi everybody,

 

On one of my pages, I have a form that a user can input data into a series of lines:

 

ex:

1:__line 1__________

2:__line 2__________

3:____________

4:__line 4__________

 

and when the form is submitted, it is echoed in the same fashion

 

ex.

line 1

line 2

 

line 4

 

I am wondering if there is a way to make it so that if a person didn't input anything in one of the lines, the following line would be echoed up a line (sort of like implode, but vertically)?

 

ex.

line 1

line 2

line 4

 

Thanks for the input

 

Link to comment
Share on other sites

If you want to remove more that one line you have to look for multiple line breaks then replace with one

 

<?php
$text = $_POST['tarea'];
$array = array("\r\n\r\n", "\n\n", "\r\r");
$replace = "\r\n";
$newtext = str_replace($array, $replace, $text);
echo nl2br($newtext);
?>

 

Ray

Link to comment
Share on other sites

Nah, you'd need to put an HTML line break.  My code was supposed to read:

 

echo implode('<br />', $list);

 

Ah yes, I was thinking the lines of a text file, not the lines of a text BOX. :P

 

I didn't think that he was putting it back into a textbox.  I think he's echoing it straight out.

Link to comment
Share on other sites

Based on memory, I think my code looks similar to this

(warning, it's not pretty at all)

 

<?php 
$query="SELECT * FROM $tbl_name WHERE id='33'";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo '<h5>', ($row['rem1']), '</h5>';
}
mysql_free_result($result);
?><br />
<?php 
$query="SELECT * FROM $tbl_name WHERE id='34";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo '<h5>', ($row['rem1']), '</h5>';
}
mysql_free_result($result);
?><br />
<?php 
$query="SELECT * FROM $tbl_name WHERE id='35'";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo '<h5>', ($row['rem1']), '</h5>';
}
mysql_free_result($result);
?><br />
<?php 
$query="SELECT * FROM $tbl_name WHERE id='36'";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo '<h5>', ($row['rem1']), '</h5>';
}
mysql_free_result($result);
?><br />

 

What I'm looking for is if someone entered values in 33, 34, and 36, but not 35, it would be echo as:

 

33

34

36

 

not

 

33

34

 

36

 

Thanks

 

Link to comment
Share on other sites

problem is you are using a bunch of queries then putting a break in. Why not use 1 query.

 

<?php
$s = array(33,34,35,36);
$values = implode("', '", $s);
$query="SELECT * FROM $tbl_name WHERE id IN ('$values')";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo '<h5>', ($row['rem1']), '</h5>';
}
mysql_free_result($result);
?><br />

 

Ray

Link to comment
Share on other sites

my bad I forgot <h> tags don't start a new line.

<?php
$s = array(33,34,35,36);
$values = implode("', '", $s);
$query="SELECT * FROM $tbl_name WHERE id IN ('$values')";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo '<h5>', ($row['rem1']), '</h5><br />';
}
mysql_free_result($result);
?>

 

Ray

Link to comment
Share on other sites

BY default header tags (<h1> to <h6>) have a default padding/margin. Because of this your numbers will be spaced apart.

 

To remove padding/margin apply the following CSS

h5 {
 padding: 0;
 margin: 0;
}

 

Or don't use a header tag to display your numbers.

Link to comment
Share on other sites

Thanks Wildteen.  I don't think I was clear on my previous post.  I apologize for that.

 

When I echo, I am looking to make it similar to how I go this implode statement to work:

$query="SELECT * FROM $tbl_name WHERE id ='10'";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result))
{
$array = array('<h5 class="update1">', ($row['up1']), '</h5>', '<h5 class="update2">', ($row['up2']), '</h5>', '<h5 class="update3">', ($row['up3']), '</h5>');
$comma_separated = implode(" ", $array);
echo $comma_separated;
} 
mysql_free_result($result);
?>

 

In that example, it was echoed on one line, and if there was nothing input in $row['up2'], it would be replaced with a " " and the proceeding input (up3) would be echoed immediately after the initial input (up1).

 

I am looking for a way so that instead of this happening on one horizontal line, it happens with one input per line.

 

Thank you for helping.

Link to comment
Share on other sites

Does each row have three columns, called upX (X being a number 1 to 3) If so do

$query = "SELECT * FROM $tbl_name WHERE id ='10'";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result))
{
    $i = 1;
    foreach($row as $item)
    {
        if(!empty($item))
        {
            echo '<h5 class="update' . $i .'">' . $item . '</h5>';
        }

        $i++;
    }
}

mysql_free_result($result);

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.