Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make an array of this

Status
Not open for further replies.

Wulfgen

Technical User
Dec 31, 2004
283
US
I have a template file used in a store, and adding this code adds a page viewable for that particular record (item)

Code:
<?php
if ($record_number == 415) {
include 'blah.php';
}
elseif ($record_number == 416) {
include 'yadayada.php';
}
elseif ($record_number == 417) {
include 'whoohoo.php';
}
?>

I was wondering if there was a way to get the $record_number/s and their corresponding pages in an array... that way I can reference them in a single line of code, rather than adding a new string for each one.....

I'm finding that I have a lot of records (over 200+) , so writing this string for each one is cumbersome:

elseif ($record_number == 416) {
include 'yadayada.php';
}
 
Where are you getting the values from?


It could be as simple as:
Code:
$myarray=array();

$myarray[415]="yadayada.php";
$myarray[416]="blah.php";

etc..
How you put it in will depend on where the numbers and php file relation is stored.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Record numbers are dynamically generated by the shop code and the pages are stored in the root level (same as index/contact etc, etc)
 
Yes but how do you know what number belongs to what page?

Creating an array is easy, but you need the relationship.

You could create the array manually, but that kind of defeats the purpose I guess.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I think that what vacunita is trying to point out is the need to have the information in a format where PHP code can scan through and dynamically include the file associated to the record number.

That said, if you had this information in a DB or text file, read the information and then loop through it.

Do you have a file of any sort where the record number and the table to be included are associated/listed?

Something like records.txt with content like.-
415,foo.php
416,bar.php
417,foobar.php

or a MySQL DB with a table holding same set of data relation

So, what do you have? If nothing, that would be the first thing to do; store the record number and include table relation on a file. Once you have that, you modify your code to loop through that file. No need to set an array, unless, of course, additional manipulation is needed where an array beats looping through the file every time you need to reference them.


 
NO I dont have any txt file with associations - a friend wrote that little bit of code to place in the template so whenever a particular product's detail information is displayed - such as "if this record number 415 (whatever the number is) - then show this page"

Thats all I have, as I add items to the cart (in the admin area) it generates a number for the item and its not consistent - if I add a category or option in between items it will number them sequentially, which means as the additions are made the number order can vary - so thats why I was using the if/elseif statements - I just dont want to have to use the entire string everytime I add a new record number.

I just figured there would be a shorter way to write it and achieve the same result.

The store has a mySql database that generates a record_number, but no matter where I look I cant find a table that has any relationship to record numbers, however, throughout the entire database there are references to adding VALUES to tables and its inserting 'record_number' into specific table names. I simply cant find a table with a list of "record numbers"

As I mentioned to Southbeach a fewposts ago, I have no working clue to mysql - I just started "fiddling" with it quite recently - reading "how-to" books is fine [I've purchased three] however, time and clients dont wait for you to "catch up". So I'm in this learn as you go method...
 
I appears that you are writing a page for each product you add to your DB. This is not a good practice, it totally defeats the server side scripting concept. That said, what you need to do is to start with a clean core or main processing page, setup your DB to hold the content of what you would display on these pages, add field to your product table to link the products to a 'content' record then, when product is queried or requested, you can retrieve the content from your table and display it.

All of this can be done with no more than a couple of scripts.

Does this sound like what you are looking for? How do you determine the relation between the record number and the target page? Try to automate this logic and write the code to do it dynamically as I mention above.

What do you think?
 
I think I'll just stick to writing if/elseif's because all I'm talking about is a page that has extra info (the same) for certain products. It will only appear when the record number needs to match (obviously I don't know what the record number on the particular item is - until I add it to the cart) - and then, if it needs the appropriate page to show, I'll just add it to the elseif string.

I was kinda hoping there would be a shorter way to write the (original) first code shown in a shorter array style format. What you are all projecting is way more than I think I need - BUT - I really appreciate your interest and help. This forum has helped me more than you can realize.
 
it seems to me that the easiest way is to add a field to your database where you can store the relevant include along with the product number, at the time that the product is created.

what i guess we're all having trouble with, from your explanation, is how you determine, dynamically, which product gets which include.

of course its easy to get a list of product numbers in an array
Code:
$result = mysql_query('select productNumber from products');
while ($row = mysql_fetch_assoc($result)){
  $productNumber[] = $row['productNumber'];
}

but assuming the include can be derived from the database you could simply do this
Code:
$result = mysql_query('select includePage from products where productNumber = '".mysql_real_escape_string($productNumber) . "'");
if (mysql_num_rows($result) > 0){
 include mysql_result($result, 0 ,0);
} else {
 include '' ;//some default page

or if the include is derived from some stable classification you could do this

Code:
$result = mysql_query ('select categorisation from products where productNumber = '".mysql_real_escape_string($productNumber) . "'");
switch (mysql_result($result, 0, 0){
  case 'category1':
   include 'something.php';
   break;
  case 'category2':
   include 'somethingelse.php';
   break;
  default:
   include 'somedefaultpage.php';
}

now ... i see that you don't know what your table structure is and how the relationships might work. we might be able to help you with this if you post the structure that you have in this thread. if it looks complex you may have to seek the assistance of our mysql brethren.

this code will give us your database structure

Code:
$output = '';
$result = mysql_query('show tables');
while ($row = mysql_fetch_assoc($result)){
  $t = mysql_result($result,0,0); //table name
  $result2 = mysql_query('show create table $t');
  $r = mysql_fetch_array($result2);
  $output .= $r[1];
}
echo "<pre>$output</pre>"; //this is what we need
 
Wulfgen, do not give up on this. Trust me, you will learn a lot and will look back and appreciate the difference it makes if you do what we are suggesting.

It may appear as cumbersome but that is because of you limited experience. Trust me on this, you are going to love it ... Just post your structure as per jpadie and lets take a stab at it.

Of course, you should mirror your site and make these changes there.
 
I agree with southbeach. don;t give up.

After re-reading your posts, it seams to me what you want to do, s some way to actually call the related page without using an if construct.

Perhaps what you were lookng to achieve was something like:

Code:
include($myarray[$recordnumber]);

This would automatically include the the page for whatever the value of $recordnumber currently is.

Of course the array would have to be populated with the pages.

You could have a file with all these, and just add in a new one when you need to. If you include this page at the top of your script then everything else is done automatically by PHP.

Code:
$myarray[214]="yadayada.php";
$myarray[216]="otherpage.php"
...

You'd still have to create the entry in the file, but you would not have to alter the code that displays it, as it would automatically display the correct one upon request.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Wulfgen,

although you don't know where $record_number comes from, you have some static code which compares somewaht to a simple table with two fields RecordNumber and IncludeFilename.

If you want to stop adding code to that page, create a new table, perhaps in a seperate database. Then you could simply fetch the one record for the $record_number and lookup the include file with some query like this:

Code:
$result = mysql_query ('select IncludeFile from includefiles where RecordNumber = ".mysql_real_escape_string($record_number));

You would need to create a new table called includefiles with fields RecordNumber (integer) and IncludeFile (char(50)), perhaps you have phpMyAdmin installed to add that table. And then populate that table with the recordnumber/includefile pairs.

That's what a database/table is about: storing repeated value structures and make them available as needed.

What you do is hardcoding a lookup table and the larger it get's the longer it will need to determine the include file, as you check each "record", if it fits instead of simply going to that record/line and looking up the include file from there.

Vacunita's one-liner include($myarray[$recordnumber]); looks even better, but you can only run that after you created the array. That would be like reading in the whole table and then fetching only one record from it.

The best would really be, you know where that record number comes from, then you could add the includefile field there. But cautious, a new field in a table can break code, a new table can too.

Bye, Olaf.
 
Grrr, woof

I'm still at a loss to get this working - most of the comments (and I sincerely thank you all for them) want me to add a field to the database (out of my league- and will break the license as well - so I cant do anything to the db - also the upgrade path will be denied as well)

However, as I mentioned - a friend wrote this (and it works) if I place this snippet into the product_detail_tpl.php
Code:
<?php
if ($record_number == 415) {
include 'blah.php';
} elseif ($record_number == 416) {
include 'yadayada.php';
}
?>
the record number is generated when Ii add an item to the database through the admin area - this snippet is added to the template page (item description) used by the cart.

It works well but I have to keep adding else/if's whenever I have more descriptions for particular items - to save this hassle i figured it could reference a txt file [more.txt] like:

$myarray[$record_number == 415]="415details.php";
$myarray[$record_number == 416]="416details.php";

and reference it like this

<?php
include($myarray[more.txt]);
?>

am I going the right way about this - all I want to do is edit the more.txt (as necessary), that way it will be easier for me. I can generate the detail page for the item when I need to.
 
OK then, try this:
1) Crate a text file, say reflist.txt
2) Data in the file should look like this
900~page900.php~eol
901~page901.php~eol
902~page902.php~eol
3) Use this code to scan through this file and load desired page
Code:
$ref_file=file("reflist.txt"); 
$OK=0;
for ($i=0; $i<sizeof($ref_file); $i++) {
  list($num, $script, $eol)=split("~", $ref_file[$i]);
  if ($num == $record_number) OK=1; break;
}
if ($OK) include($script);
if (!$OK) echo 'Expected script not listed in reference file!<br /';


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
I get a Parse error: syntax error, unexpected '=' in (the template file)

I made the txt file, referenced it to a test php fiile, and set the 900 to a (record_number) 282 and this is what I get...
 
OK so I got around it - I simply added [ include 'reflist.php'; ] to the template and in the reflist.php I put the script i previously used.

Now all I do is make the necessary details pages and change the pointer to the record and page in reflist.php page. Fixed, yay!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top