homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Using Arrays
Assigning arrays to values from Mysql Table

 9:52 pm on Apr 14, 2014 (gmt 0)

Thank you for reading my post. I am not a programmer but have managed at my age to get along by copying and pasting code and getting things to work.

I am stuck.. i want to assign values that I pulled out of an database and applied to css Tabs so that i can use it again on later the same page. My problem is in the second area i want to use it i cannot pull the content the same way as in that area i am pulling other data from the database.

see the script

<ul class="nav nav-pills nav-justified">
<li class="active"><a href="#000" data-toggle="tab"><i class="icon-new-tab2"></i> Featured</a></li>
<?php $cattid = $_GET['ListingID'];?>
<?php include("inc_db/inc_connect.php"); ?>
<?php $table="c_select_sub_cat";;?>
mysql_connect ($db_host,$db_username,$db_password);
@mysql_select_db ($db_name);
$result = mysql_query("select cat_sub_area_id,cat_sub_area_title,cat_sub_tabs_order from $table where cat_area_id='$cattid'" );
$num_fields = mysql_num_fields($result);
$num_rows = mysql_num_rows($result);
$row_cnt = 0;
while ($num_rows>$row_cnt)
$record = @mysql_fetch_row($result);
<li><a href="#<?php print "$record[2]"; ?>" data-toggle="tab"><i class="icon-menu2"></i> <?php print "$record[1]"; ?></a></li>
<?php $row_cnt++;} ?>
<?php mysql_close(); ?>

This works perfect creating my Tabs

There will always be four records each time i access the table but i want to use those values again...

I tried using

$SitetabeValue = $record[1];

But it repeats the same record over and over
Not sure how arrays work but it seems therein lies my answer !

any help advice would be appreciated!



 3:19 pm on Apr 15, 2014 (gmt 0)

The easiest way is probably just to assign your data to another variable...

$data = array(); // Save all your data here 
while ($num_rows>$row_cnt)
$record = @mysql_fetch_row($result);
$data[] = $record;

$data now holds all your data from the four records...

echo $data[2][1]; // 3rd record, 2nd field (arrays start at 0)

Later in your code, to step through this data...

foreach ($data as $record) { 
// 1st field, 2nd field (same idea as your first example)
echo $record[0].$record[1];


 3:59 pm on Apr 15, 2014 (gmt 0)

Dear Penders !

Thank you... you have given me the correcting wording i need to explain this properly!

I want the data to be available outside of the stepping thing... in other words once i have closed the connection and the loop or steps have run their course i want those records available in another place...Outside the Stepps(lol) Where they are they are being used for the purpose(Jquery Tabs..) so i need them without stepping through loops or "while" or "if"'s

Let me try an explain it this way !

(1)I have 4 tabs.. the script above calls the titles for those tabs! - it works well

(2)The body and content of each tab i want to call data from another table into those tabs. the thing is i cannot call the data properly because i need those 4 or 5 records to serve as filter to call the correct data from the other table!

See this page

Each tab has its own script calling data from the same table but filtered with the headings of the tabs

That is why i need the tabs headings or titles again without loop or stepping them through anything

Thank you

[edited by: incrediBILL at 6:21 pm (utc) on Apr 20, 2014]
[edit reason] URls removed. No site reviews. Please see forum charter and TOS [/edit]


 6:16 pm on Apr 15, 2014 (gmt 0)

I want the data to be available outside of the stepping thing...

That was why I assigned the table data to the variable
$data - this is then available later in the page, wherever you want to use it. But by the very nature of this data (a series of records and fields) you are likely going to have to loop/step through it in order to be able to use it? Unless you generate your HTML in the initial data retrieval stage (assign this to a variable) and simply output the HTML later?

You don't need to step through the $data array if you just want to extract a single value from it. Like in my example above:

If you need this data in an entirely different part of your site then you are likely going to have to open the database connection again to retrieve this information.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved