PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old September 4th, 2002, 12:18 PM
freddymio freddymio is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: La Baule, France
Posts: 22 freddymio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to freddymio
TRIM SPACES WHEN DISPLAYED

Hello,
I get 4 fields from 2 different tables when I treat each line and fill a <select><option> tag, spaces are trimmed out unaligning the output.
I tried str_pad() and substr(), have managed to use Monospace fonts for the select output, and I have also asigned the field values to a var before making it a part of the Options.
I'm persuing an aligned option list:
php Code:
Original - php Code
  1.  
  2. Client name   |City name|Code
  3. Clent name A  |City nam |Code A
  4. Client name XA|City x   |Code WQZ

I have requested your help with the following code, although the vars in the code don't reflect the example:
php Code:
Original - php Code
  1.  
  2. <?php
  3. echo "<select name="id_name">";
  4. while (list ($id,$nom,$ville,$region)=$db_link->get_data()) //External class; works!
  5. {
  6.     $id=str_pad($id,10);
  7.     $nom=str_pad($nom,20);
  8.     $ville=str_pad($ville,15);
  9.     $region=str_pad($region,15);
  10.     $the_selection=$nom."|".$ville."|".$region."|".$id;
  11.     echo "<option>".$the_selection."</option>";
  12. }
  13. echo "</select>";
  14. ?>

Thank you,
Freddy

Reply With Quote
  #2  
Old September 4th, 2002, 12:41 PM
Kohaar Kohaar is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Denmark
Posts: 147 Kohaar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 37 sec
Reputation Power: 2
Send a message via ICQ to Kohaar
RE: TRIM SPACES WHEN DISPLAYED

Try this:

<code>
<?
$string = $str_replace(" ", "&nbsp;", $string);
?>
</code>

Reply With Quote
  #3  
Old September 4th, 2002, 12:43 PM
Nimco Nimco is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 132 Nimco User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to Nimco
RE: TRIM SPACES WHEN DISPLAYED

The problem is that in the web page, the browser ignores white space more than a single space. To enter multiple spaces, you need to use '&nbsp;'. However, if you just tried padding with this, it wouldn't work, since it sees '&nbsp;' as 5 chars not 1. Thus you will need to run an str_replace() function on the variables:

php Code:
Original - php Code
  1.  
  2. <?
  3.  $id=str_pad($id,10);
  4.  $id=str_replace(" ", "&nbsp;", $id);
  5. ?>

Reply With Quote
  #4  
Old September 4th, 2002, 12:43 PM
Kohaar Kohaar is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Denmark
Posts: 147 Kohaar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 37 sec
Reputation Power: 2
Send a message via ICQ to Kohaar
RE: TRIM SPACES WHEN DISPLAYED

Sorry... That should of couse be:

Code:
<?
$string = str_replace(" ", "&nbsp;", $string);
?>

Reply With Quote
  #5  
Old September 4th, 2002, 01:28 PM
freddymio freddymio is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: La Baule, France
Posts: 22 freddymio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to freddymio
RE: TRIM SPACES WHEN DISPLAYED

The visual result I get is great; but I'm not retreiving data from the mysql SELECT statement after the str_replace function. The variable used for the SQL statement is transformed to a length of 10 that may not be the original length
php Code:
Original - php Code
  1.  
  2. <?
  3. $id=str_pad($id, 10);
  4. $id=str_replace(" ", "&nbsp;", $id);
  5.  
  6. /* To get back the original value I have done the following */
  7. $id=str_replace("&nbsp;","",$id);
  8. //or
  9. $id=trim(str_replace("&nbsp;","",$id));
  10. //or
  11. $id=trim(str_replace("&nbsp;"," ",$id));
  12. //or simply
  13. $id=trim($id);
  14. ?>

I still get a value of 10 in strlen($id).
Help help. Thank you for your help,
Freddy

Reply With Quote
  #6  
Old September 4th, 2002, 01:43 PM
Nimco Nimco is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 132 Nimco User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to Nimco
RE: TRIM SPACES WHEN DISPLAYED

I'm not quite sure what you are saying the problem is? I thought what you were trying to do was to visually align the <option>s in the <select> dropdown? What are you trying to do now?

Reply With Quote
  #7  
Old September 4th, 2002, 01:45 PM
Nimco Nimco is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 132 Nimco User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to Nimco
RE: TRIM SPACES WHEN DISPLAYED

OK, I think I understand. You're problem is trying to get back to the initial variable. Yes? I'm just playing around now trying to get it to work...

Reply With Quote
  #8  
Old September 4th, 2002, 01:49 PM
Nimco Nimco is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 132 Nimco User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to Nimco
RE: TRIM SPACES WHEN DISPLAYED

OK, I just tried your examples, and the first one you put down worked:

php Code:
Original - php Code
  1.  
  2. <?
  3. $id=str_replace("&nbsp;","",$id);
  4. echo strlen($id); // prints correct length without spaces
  5. ?>

Reply With Quote
  #9  
Old September 4th, 2002, 01:49 PM
freddymio freddymio is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: La Baule, France
Posts: 22 freddymio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to freddymio
RE: TRIM SPACES WHEN DISPLAYED

Thank you for replying:
I had a select option that when SELECTED by the user got "explode"d to some vars and I got the selected ID code, then I used this ID to query MySQL and the existing data got retreived.
The alignment in the select/option did not work, now the alignment is very good, but no data is retreived from the query. Thank you in advance.

Reply With Quote
  #10  
Old September 4th, 2002, 01:51 PM
Nimco Nimco is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 132 Nimco User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via AIM to Nimco
RE: TRIM SPACES WHEN DISPLAYED

What was the exact command you were using to explode the string? Post it here along with an example of what it does start with and return, along with what you want it to start with and return.

Reply With Quote
  #11  
Old September 4th, 2002, 02:20 PM
freddymio freddymio is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: La Baule, France
Posts: 22 freddymio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to freddymio
RE: TRIM SPACES WHEN DISPLAYED

Sorry, it doesn't work in my code. I'm posting some fragments of it.
php Code:
Original - php Code
  1.  
  2. <?
  3. /* At the beginning of the file; it is a $_SERVER['PHP_SELF'] posted form */
  4. if (isset($_POST['id_name']))
  5. {
  6.     list($_SESSION['affected_name'], $_SESSION['affected_city'], $_SESSION['affected_region'],
  7. $_SESSION['affected_id'])=explode("|",$_POST['id_name']);
  8. // vvvvv PROBLEM vvvvv
  9.     $_SESSION['affected_id']=str_replace("&nbsp;","",$_SESSION['affected_id']);
  10. // ^^^^^ PROBLEM ^^^^^
  11.     header("location:param_activity.php");
  12. }
  13.  
  14. /* In the form */
  15. echo "<select name="id_name">";
  16. while(list($id, $nom, $ville, $region)=$db_link->get_data())
  17. {
  18.     $nom=str_pad($nom,20);
  19.     $ville=str_pad($ville,15);
  20.     $region=str_pad($region,15);
  21.     $id=str_pad($id,10);
  22.     $nom=str_replace(" ","&nbsp;",$nom);
  23.     $ville=str_replace(" ","&nbsp;",$ville);
  24.     $region=str_replace(" ","&nbsp;",$region);
  25.     $id=str_replace(" ","&nbsp;",$id);
  26.     $the_selection=$nom."|".$ville."|".$region."|".<