473,320 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Only the first character shows up after assigning one var to another

Expand|Select|Wrap|Line Numbers
  1.  <?php $counter = 1 ; ?>
  2.  <?php foreach( $data as $row ) { ?>
  3.  <tr>
  4.     <td> 
  5.       <?php $name[$counter]=$row['name'];?>
  6.       <?php echo( $row['name'] ); ?>            <!-- I get Brooklyn -->      
  7.       <?php echo( $name[$counter] ); ?>        
  8.         <!-- I get only first letter B -->
  9.     </td> 
  10.  
shouldn't they both be the same??
Feb 13 '11 #1
4 5586
This depends on what is in your $data array, if it's a array of strings what you mentioned above could happen.
Other wise if it's a array of arrays with 'name' element in each, it should work.

Expand|Select|Wrap|Line Numbers
  1. $data=array(
  2.     array('name'=>'Bytes.com'),
  3.     array('name'=>'PHP'),
  4.     array('name'=>'Question'),
  5.     array('name'=>'908173'),
  6. );
Feb 13 '11 #2
Samishii23
246 100+
First off, as a general coding practice, try to do a little amount of <?php ?> in your page as possible. This reduces performance drastically. 2nd you don't need to put () around anything you want to echo.
Expand|Select|Wrap|Line Numbers
  1. $counter = 1 ;
  2. echo $name[$counter];
If your only getting the 1st character of the $name in your foreach iteration, then whatever is being returned through the $name variable is a string.

In PHP if you want a specific character of a string variable, you can access it as if it were an array.
Expand|Select|Wrap|Line Numbers
  1. $str = 'hello world';
  2. echo $str[0]; // h
  3. echo $str[6]; // w
Feb 15 '11 #3
I think that adding this inside at the end of your foreach-loop will fix your problem:
Expand|Select|Wrap|Line Numbers
  1. $counter++;
If it doesn't help, I haven't really understood your problem, and will reread it after you give me feedback. :) Good luck to you, mister!
Feb 17 '11 #4
Well, its bad practice to load an array with a counting integer anyway, i suggest you use:

$name[]=$row['name'];

the increment is automatic. Sorry i didnt read the rest I just saw something to tip you about. :)
Feb 20 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Gary | last post by:
Morning all, I have a form field called: Bsk01 How do I onBlur prompt the user to enter a ZERO as character one, if one is not already entered. At the same time, I would like to ensure at...
2
by: D.Frangiskatos | last post by:
Hi, I have been working for a few months in project that deals raw sockets. However recently, and while trying to examine the contents of the buffer used in recvfrom i was a bit confused. The...
0
by: seanmayhew | last post by:
Im having a small problem with my Dataview sort. Its sorting correctly on the first character but after thats it seems random so for a state list at the "W's" it looks like this: WA WV WY WI...
2
by: bmayer | last post by:
I am using a SqlDataSource to put data into a database from three TextBoxes. The problem is that only the first character from the TextBoxes is being inserted into the database. The code: ...
12
by: Frank | last post by:
There must be an easier way then what I'm doing to determine if the first character in a string is a valid letter. My code is getting to big. There must be a better way. Thanks in advance
5
by: manu99 | last post by:
hi all, I have developed a console application with MFC and unicode supported using Visual studio.net 2003. The problem I have is that the output stream is displaying only the first character in...
9
by: sovht | last post by:
System: Intel, Windows XP Pro, SP2 IDE: VC++ 6.0 Problem: *Very* simple program to create a MessageBox only ever displays the first character of the given string. I checked the spec for the...
6
by: Academia | last post by:
I want to search for Dim and replace it with Dim That is, I want to change the first character of Dim variable names to upper case. I can't figure know to use Regular Expression to do that....
1
by: bdbeames | last post by:
I have a form where I would like to perform some validation using a javascript function. I cant' figure out the RegExp. function is_number(target) { target.value = trim(target.value); ...
2
by: ahmadasghar | last post by:
Hello, I am a beginner at C# and am using it to develop an application to control a robot remotely. To communicate with the robot I am using UDP. I am able to successfully send instructions to it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.