Use Arrays ?

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 pretty nicely.

Though…I am stuck… I want to assign values that I pulled out of an database, 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.

My PHP script
[php]

  • Featured
  • <?php $cattid = $_GET['ListingID'];?> <?php include("inc_db/inc_connect.php"); ?> <?php $table="c_select_sub_cat";;?> <?php 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); ?>
  • " data-toggle="tab"> <?php print "$record[1]"; ?>
  • <?php $row_cnt++;} ?> <?php mysql_close(); ?>
[/php]

This works perfect creating my Tabs

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

I tried using
[php]$SiteTabValue1 = $record[1];
$SiteTabValue2 = $record[1];
$SiteTabValue3 = $record[1];
$SiteTabValue4 = $record[1];[/php]
below line 18 to define each value from the database to a stored value but it repeats the same record over and over

any help advice would be appreciated!
Regards
Sam

I would suggest hiring someone to clean this up.

The loop logic to pull data from the db is kinda strange, and the db query itself is using deprecated functions and is vulnerable to sql injection, meaning anyone can dump your entire database including usernames and passwords if you store them.

Jim…

Thank You, I went and read up on the issue and found this way using the PDO… is it better?

[php]<?php
$hostdb = ‘localhost:3307’;
$namedb = ‘asecret’;
$userdb = ‘asecret’;
$passdb = ‘asecret’;
$table = “c_select_cat”;
try {
$conn = new PDO(“mysql:host=$hostdb; dbname=$namedb”, $userdb, $passdb);
$conn->exec(“SET CHARACTER SET utf8”);// Sets encoding UTF-8
$sql = “SELECT * FROM $table”;
$result = $conn->query($sql);
if($result !== false) {
$cols = $result->columnCount();// Number of returned columns
// Parse the result set
foreach($result as $row) {
?>

ID:<?php echo $row['cat_id'];?> - Title:<?php echo $row['cat_title'];?>">
<?php } } $conn = null; // Disconnect } catch(PDOException $e) { echo $e->getMessage(); } ?>[/php]

So… if this code isw safer, and then my question is how do I store each of the results as a variable or stored items so that i can use it further down on my page ?

For example:

If the results from this query was as follows
ID: 1 - Title:alpha (? assign this result to $variable1)
ID: 2 - Title:beta (? assign this result to $variable2)
ID: 3 - Title:charlie (? assign this result to $variable3)
ID: 4 - Title:delta (? assign this result to $variable4)

I want to after i closed the loop and database connection still have these items available !

so that i can use them l using the <?php echo ...... ?>

Thank you

Just run the id’s through a loop, a for or foreach loop would do the trick. Something like
[php]$SiteTabValue = array();
foreach($record as $val) {
$SiteTabValue[] = $val;
}

// or
$SiteTabValue = array();
for($i=0; $i < count($record); $i++) {
$SiteTabValue[] = $record[$i];
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service