473,473 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

break apart a variable

I would like to know how I can break apart a variable. I'm currently
passing the variable $id-hours and I would like to break it down. I would
like to break down to something similar to:

$eid = id (id part of $id-hours)
$ehour = hours (hours part of $id-hours)

Is this possible? I'm new to programming and just trying to get thru it
all. Thanks for any help that you may be able to provide.

A
Feb 7 '07 #1
13 1729
Rik
Auddog <wi****@hotmail.comwrote:
I would like to know how I can break apart a variable. I'm currently
passing the variable $id-hours and I would like to break it down. I
would
like to break down to something similar to:

$eid = id (id part of $id-hours)
$ehour = hours (hours part of $id-hours)
How is $id-hours formatted?
Is this possible? I'm new to programming and just trying to get thru it
all. Thanks for any help that you may be able to provide.
Several functions come to mind. You might want to use one of these:
- explode()
- fscanf()
- split()
- preg_match()

Not much else I can say without knowing the format.

Also, why do you have this in 1 variable? More logical would be:

$var = array('id'='your_id','hours' ='your_hours', 'foo' ='bar');
--
Rik Wasmus
Feb 7 '07 #2
Auddog wrote:
I would like to know how I can break apart a variable. I'm currently
passing the variable $id-hours and I would like to break it down. I would
like to break down to something similar to:

$eid = id (id part of $id-hours)
$ehour = hours (hours part of $id-hours)

Is this possible? I'm new to programming and just trying to get thru it
all. Thanks for any help that you may be able to provide.

A

Not easily. When you pass a variable to a function the name of the
variable changes.

But why would you need to, anyway? Variable names are just there for
human readability; they should be meaningful to the programmer, but the
computer doesn't care.

And BTW - $id-hours is an invalid variable name. The hyphen isn't valid
in a variable name.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 7 '07 #3
My form is created by querying my database for id, fname, lname of employees
and then I add an input box for hours worked. I want everyone that works on
the production lines listed. I then would like to be able to insert the
form information into a database. I just don't know how to do this. I'm
grasping at straws right now.

Here is my form code:
<?php

//create query
$query = "SELECT id, fname, lname FROM prod_employee where active = 'yes'";

//excute query
$result = mysqli_query($connection, $query) or die ("Error in query: $query.
".mysqli_error());

// see if any rows were returned
if (mysqli_fetch_array($result) 0) {
// yes
// print them one after another
while (list($id, $fname, $lname) = mysqli_fetch_row($result))
{
echo " <tr>
<td><div align=center>$id</div></td>
<td>$fname</td>
<td>$lname</td>
<td><input name=$id-hours type=text size=4 maxlength=4 />
</tr>";
}
echo "</table>";
}
else {
// no
// print status message
echo "No rows found!";
}

Again thanks for any help that you can provide.

A
"Rik" <lu************@hotmail.comwrote in message
news:op***************@misant.kabel.utwente.nl...
Auddog <wi****@hotmail.comwrote:
I would like to know how I can break apart a variable. I'm currently
passing the variable $id-hours and I would like to break it down. I
would
like to break down to something similar to:

$eid = id (id part of $id-hours)
$ehour = hours (hours part of $id-hours)
How is $id-hours formatted?
Is this possible? I'm new to programming and just trying to get thru it
all. Thanks for any help that you may be able to provide.
Several functions come to mind. You might want to use one of these:
- explode()
- fscanf()
- split()
- preg_match()

Not much else I can say without knowing the format.

Also, why do you have this in 1 variable? More logical would be:

$var = array('id'='your_id','hours' ='your_hours', 'foo' ='bar');
--
Rik Wasmus
Feb 7 '07 #4
Rik
Auddog <wi****@hotmail.comwrote:
My form is created by querying my database for id, fname, lname of
employees
and then I add an input box for hours worked. I want everyone that
works on
the production lines listed. I then would like to be able to insert the
form information into a database. I just don't know how to do this. I'm
grasping at straws right now.

Here is my form code:
<?php

// see if any rows were returned
if (mysqli_fetch_array($result) 0) {
This would mean you always discard the first row, use
mysqli_num_rows($result) instead.

while (list($id, $fname, $lname) = mysqli_fetch_row($result))
{
echo " <tr>
<td><input name=$id-hours type=text size=4 maxlength=4 />
</tr>";
}
echo "</table>";
}
I'd do this:

echo '<input name="hours['.$id.']" type="text" size="4" maxlength="4" >';

And upon receiving, you'll have an array in $_POST (or $_GET, but don't do
that) named 'hours' containing the id as key and the value as the input.
Check this with print_r($_POST['hours']);

To keep using the current form, you'd have to do something like this:

$hours = array();
foreach($_POST as $key =$value){
if(preg_match('/^[0-9]+-hours$/i',$key){
$keyarray = explode('-',$key);
$hours[$keyarray[0]] = $value;
}
}
print_r($hours);
--
Rik Wasmus
Feb 7 '07 #5
Rik,

Thanks for the feed back and here is what I got from it. By placing the [ ]
around the $id (what's the periods for???) it will create an array for
$hours. What is the key? I see you assign the id as it's value, is that
html or php? Lastly, when I execute the code all I get on the screen is
Array ( ). I can place all my code here if it would help. Thanks again for
all your help, it is greatly appreciated.

A

Sorry for all the questions, but I want to understand what each line is
doing so that I can get this figured out.
"Rik" <lu************@hotmail.comwrote in message
news:op***************@misant.kabel.utwente.nl...
Auddog <wi****@hotmail.comwrote:
My form is created by querying my database for id, fname, lname of
employees
and then I add an input box for hours worked. I want everyone that works
on
the production lines listed. I then would like to be able to insert the
form information into a database. I just don't know how to do this. I'm
grasping at straws right now.

Here is my form code:
<?php

// see if any rows were returned
if (mysqli_fetch_array($result) 0) {
This would mean you always discard the first row, use
mysqli_num_rows($result) instead.

while (list($id, $fname, $lname) = mysqli_fetch_row($result))
{
echo " <tr>
<td><input name=$id-hours type=text size=4 maxlength=4 />
</tr>";
}
echo "</table>";
}
I'd do this:

echo '<input name="hours['.$id.']" type="text" size="4" maxlength="4" >';

And upon receiving, you'll have an array in $_POST (or $_GET, but don't do
that) named 'hours' containing the id as key and the value as the input.
Check this with print_r($_POST['hours']);

To keep using the current form, you'd have to do something like this:

$hours = array();
foreach($_POST as $key =$value){
if(preg_match('/^[0-9]+-hours$/i',$key){
$keyarray = explode('-',$key);
$hours[$keyarray[0]] = $value;
}
}
print_r($hours);
--
Rik Wasmus
Feb 7 '07 #6
Rik
Auddog <wi****@hotmail.comwrote:
Rik,

Thanks for the feed back and here is what I got from it. By placing the
[ ]
around the $id (what's the periods for???) it will create an array for
$hours.
Nonono.

By creating the _string_ with the id in it, you html output with for
instance id = 12 would looke like:

<input name="hours[12]" value="somevalue">

Which on return will be interpreted by PHP as:

_POST Array
(
'hours' =Array
(
12 ='somevalue'
)
)

--
Rik Wasmus
Feb 7 '07 #7
Here is what my input looks like from my browser (view page source):

<input name=hours['.3.'] type=text size=4 maxlength=4 >

Is that the same? I'm sorry for being slow at getting this.

A
"Rik" <lu************@hotmail.comwrote in message
news:op***************@misant.kabel.utwente.nl...
Auddog <wi****@hotmail.comwrote:
Rik,

Thanks for the feed back and here is what I got from it. By placing the
[ ]
around the $id (what's the periods for???) it will create an array for
$hours.
Nonono.

By creating the _string_ with the id in it, you html output with for
instance id = 12 would looke like:

<input name="hours[12]" value="somevalue">

Which on return will be interpreted by PHP as:

_POST Array
(
'hours' =Array
(
12 ='somevalue'
)
)

--
Rik Wasmus
Feb 7 '07 #8
Rik
Auddog <wi****@hotmail.comwrote:
Here is what my input looks like from my browser (view page source):

<input name=hours['.3.'] type=text size=4 maxlength=4 >

Is that the same? I'm sorry for being slow at getting this.
No, it is not. I gave you the code. Copy/paste it if need be.

--
Rik Wasmus
Feb 7 '07 #9
OK, I see the errors of my way. I now get :

<input name="hours[20]" type="text" size="4" maxlength="4">

I cut and pasted you code into the top part of my page - here is the code:

<?php

if (isset($_POST['submitted'])) {

$hours = array();
foreach($_POST as $key =$value){
if(preg_match('/^[0-9]+-hours$/i',$key)){
$keyarray = explode('-',$key);
$hours[$keyarray[0]] = $value;
}
}
print_r($hours);
//if - else statement isset
} else {

?>

When I run this all I get is array ( ) - is there something else I'm
missing.

"Rik" <lu************@hotmail.comwrote in message
news:op***************@misant.kabel.utwente.nl...
Auddog <wi****@hotmail.comwrote:
Here is what my input looks like from my browser (view page source):

<input name=hours['.3.'] type=text size=4 maxlength=4 >

Is that the same? I'm sorry for being slow at getting this.
No, it is not. I gave you the code. Copy/paste it if need be.

--
Rik Wasmus
Feb 7 '07 #10
Rik
Auddog <wi****@hotmail.comwrote:
When I run this all I get is array ( ) - is there something else I'm
missing.
Yes, before that piece of code I said:"To keep using the current form".
This , to me, would mean fully spelled out: "If you do not want to change
your form like this, and keep on using the form you allready have, then I
suggest the following approach.

In short: I gave to answers: one to alter the form and have an easier time
to process it, one how to process the current form you have now.

Please try to read answers more carefully, and when something doesn't
work, reread them to make sure you did not miss anything.
--
Rik Wasmus
Feb 7 '07 #11
Auddog wrote:
"Rik" <lu************@hotmail.comwrote in message
news:op***************@misant.kabel.utwente.nl...
>Auddog <wi****@hotmail.comwrote:

Here is what my input looks like from my browser (view page source):

<input name=hours['.3.'] type=text size=4 maxlength=4 >

Is that the same? I'm sorry for being slow at getting this.

No, it is not. I gave you the code. Copy/paste it if need be.

OK, I see the errors of my way. I now get :

<input name="hours[20]" type="text" size="4" maxlength="4">

I cut and pasted you code into the top part of my page - here is the code:

<?php

if (isset($_POST['submitted'])) {

$hours = array();
foreach($_POST as $key =$value){
if(preg_match('/^[0-9]+-hours$/i',$key)){
$keyarray = explode('-',$key);
$hours[$keyarray[0]] = $value;
}
}
print_r($hours);
//if - else statement isset
} else {

?>

When I run this all I get is array ( ) - is there something else I'm
missing.
Let's see what you get, alter Rik's code and post the output back to
us (if it repeats a lot, just post enough to get a look at what the
keys look like):

$hours = array();

echo '<pre>';
print_r($_POST); // dump all submitted POST data
echo '</pre><br>';

foreach($_POST as $key =$value){
if(preg_match('/^[0-9]+-hours$/i',$key)){
$keyarray = explode('-',$key);
$hours[$keyarray[0]] = $value;
}
}
print_r($hours);

Feb 8 '07 #12
Rik
Curtis <ze******@verizon.netwrote:
Let's see what you get, alter Rik's code and post the output back to
us (if it repeats a lot, just post enough to get a look at what the
keys look like):

$hours = array();

echo '<pre>';
print_r($_POST); // dump all submitted POST data
echo '</pre><br>';

foreach($_POST as $key =$value){
if(preg_match('/^[0-9]+-hours$/i',$key)){
The would only be usefull if the postfields were still in the 'id-hours'
format. It's now in 'hours[id]'.

So just print_r($_POST['hours']);
--
Rik Wasmus
Feb 8 '07 #13
I went back and did as you told me. I was assuming the second option was
what was happening after you hit the submit button. I apologize for not
following the instructions,. I guess I'm biting off more than I know with
this script.

I was able to change my page and I did start getting the results: Array (
[1] =8 [2] =[3] =[4] =[5] =8 ETC. I'm still curious as to how the
=got into the mix, but I'll do some reading to figure that out. Now that
I'm getting the something, maybe I'll be able to get it into my database.
Thanks again for all your time and help.

A
"Rik" <lu************@hotmail.comwrote in message
news:op***************@misant.kabel.utwente.nl...
Curtis <ze******@verizon.netwrote:
Let's see what you get, alter Rik's code and post the output back to
us (if it repeats a lot, just post enough to get a look at what the
keys look like):

$hours = array();

echo '<pre>';
print_r($_POST); // dump all submitted POST data
echo '</pre><br>';

foreach($_POST as $key =$value){
if(preg_match('/^[0-9]+-hours$/i',$key)){
The would only be usefull if the postfields were still in the 'id-hours'
format. It's now in 'hours[id]'.

So just print_r($_POST['hours']);
--
Rik Wasmus
Feb 8 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Glen Wheeler | last post by:
Hello All. I've been coding in python for a reasonable amount of time now (coding in total for approx. 20 years) and am writing a performance intensive program in python. The problem I'm having...
4
by: Tony Vasquez | last post by:
var _section = location.search ? location.search.split("?") : "latest" What does this line of code set ? Here is the code from which it came from... var _section = location.search ?...
3
by: yezi | last post by:
Hi: I want to break a string like "30 23 date" into 3 parts in C: int 30; int 23; char string date; Do not know how to get it.
14
by: serrand | last post by:
Could someone tell me a beautiful way to exit from a switch and a loop in one statement ... without using a goto... and if possible without using an auxiliary variable as i did... int res;...
2
by: rtilley | last post by:
Hi, While trying to better understand security descriptors on Windows. I've been examining text-based security descriptors. I have the security descriptors on individual lines stored in a text...
26
by: Alexander Korsunsky | last post by:
Hi! I have some code that looks similar to this: -------------------------------------------- char array = "abcdefghij"; for (int i = 0; i < 10; i++) {
3
by: Noozer | last post by:
Small php question here... I have a string variable, holding the results from a <textareaof a POSTed form. I want to create an array from this variable, with each element containing one line of...
3
by: Donkeyoz | last post by:
Hi all, Trying to do something fairly basic but am getting caught up on it. I am trying to extract text from a .txt file into an array line by line and then dissect that line into various parts,...
16
by: lovecreatesbea... | last post by:
Are the following two lines equal? Suppose the expression i++ doesn't overflow. They behave differently in my code. if (!p ? i++ : 0) break; if (!p){ i++; break;} Thank you for your time.
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.