Forum Moderators: coopster

Message Too Old, No Replies

creating a dynamic table cells data

         

ahmed24

8:01 am on Jul 14, 2009 (gmt 0)

10+ Year Member



I have a variable called $title that basically dynamically echos information like this:

Period1-MTW-ENG-wA-Details=Test
Period2-MF-ENG-wA-Details=Test
Period3-TW-ENG-wA-Details=Test

I have a table that i have uploaded a snapshot of here: <snip>

what i want to be able to do is maybe convert the results of the $title into some sort of variables so i can slot them into the relavant cell(s) in the table. For example: The first line result of $title needs to be placed into the table for Period1, wA on Monday Tuesday and Wednesday. Oh and the actual information that needs to be placed in the cell itself is Details=Test minus the "Details="

Does anyone have any idea how i can possibly do this?

I would really much appreciate anyones help in this.

Thanks

[edited by: dreamcatcher at 8:54 am (utc) on July 14, 2009]
[edit reason] No personal urls, thanks. [/edit]

ahmed24

12:07 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



Anyone?

coopster

12:24 pm on Jul 14, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



One approach may be to use regular expressions to parse the text in the title and build hash tables (associative arrays) which you could then loop through to build your html tables.

ahmed24

2:12 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



thanks for your reply, any guidance on how i can achieve this? code samples or links and references maybe?

thanks

jatar_k

2:30 pm on Jul 14, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



based off of your loose description

if your cells are named, or at least numbered in your iterations then you can use the numbers out of the rows to pull them.

if those strings are all the same with Period# then you could pull the number out of that string to use as the array position

if the only data is what is after the = then split on =

<?php
$myvar = 'Period1-MTW-ENG-wA-Details=Test';
$myvararr = split('=',$myvar);
$myvalue = $myvararr[1];
$myvararr = explode('-',$myvararr[0]);
$myvar2 = str_replace('Period','',$myvararr[0]);
$myendarr[$myvar2] = $myvalue;

echo '<pre>';
print_r($myendarr);
echo '</pre>';
?>

or in a loop

<?php
$strarr = array('Period1-MTW-ENG-wA-Details=Test1','Period2-MF-ENG-wA-Details=Test2','Period3-TW-ENG-wA-Details=Test3');

foreach ($strarr as $myvar) {
$myvararr = split('=',$myvar);
$myvalue = $myvararr[1];
$myvararr = explode('-',$myvararr[0]);
$myvar2 = str_replace('Period','',$myvararr[0]);
$myendarr[$myvar2] = $myvalue;
}

echo '<pre>';
print_r($myendarr);
echo '</pre>';
?>

ahmed24

3:31 pm on Jul 14, 2009 (gmt 0)

10+ Year Member




thanks jatar_k for that. using your loop example, i am now able to determine what period column the data belongs to but each one can also need to be displayed in the other rows in the same period column.

In my table, there are basically 3 period columns and 5 rows below them to indicate Monday-Friday. The days are identified from the array by the letters MTWHF. so for example:

Period1-MTW-ENG-wA-Details=Test needs to display The value Test in the Column that is Period1-wA not period1-wB and it actually needs to be displayed in the rows Monday, Tuesday and Wednesday.

any ideas how i can do this?

jatar_k

3:35 pm on Jul 14, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



in the loop example they are available after the explode in $myvararr[1], you could char by char it and put them somewhere

if you wanted to be complicated, based on what you have already you could construct an array per day and easily slot your pieces into them

you can easily expand the grab after I stripped out the string 'Period' as well

it was an example of how, you will need to adapt and tailor it to your exact situation

cross collating arrays is always fun, especially when you make a mistake, it replicates ;)

ahmed24

3:37 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



jatar_k, thats great i'll give it a try now. many thanks

ahmed24

4:00 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



sorry jatar_k, if i wanted to use your above loop example but instead of determining the period number i want to split it so that it loops the days. so for example if i had Period1-MW-ENG-wA-Details=Test1

currently in example loop code, it will result in the following being displayed: [1] => Test1

but if thats split by the days then it would read

[M] => Test1
[W] => Test1

jatar_k

4:05 pm on Jul 14, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it is looping the titles so since there would be more than one entry per title you would need to add another array inside the existing one

your loop would encompass these rows

// this gets the value from the end of the string
$myvar2 = str_replace('Period','',$myvararr[0]);
// this creates your array
$myendarr[$myvar2] = $myvalue;

you would need to change the last line and add some lines before that to walk through the days of the week

ahmed24

4:24 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



thanks jatar_k. just one last question. i seem to get all the results listed with the following added to it:

Array
(

)

isnt there no way to remove it?

thanks

jatar_k

5:11 pm on Jul 14, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe

ditch this from the end

echo '<pre>';
print_r($myendarr);
echo '</pre>';