Page 1 of 1
Storing Variables in Database
Posted: Sat Sep 18, 2010 6:55 pm
by dinocajic
OK I hope that my question will make sense (Also, hey guys I'm Dino *new member*)
I'm working on a php script and have hit a deadlock.
The company that I'm working for has a program where they keep all of their items. They wanted to make those items available online, so I contacted the company that made the program and asked them if there was any way to link it to the site, such as an export. They said that they could create a list of variables for me. So, that's what they did. They created a small addition to the program that basically exports a variable list:
Code: Select all
$item1="3"; //where the item1 variable will store the current amount.
$item2="0";
$item3="4";
$item4="10";
etc.
The following code was saved into an inventoryDocument.php and called upon throughout the website by first including the inventoryDocument.php with:
Code: Select all
<?php include 'inventoryDocument.php'; ?>
and then calling it where I need it with
Well now I'm trying to basically enter it into a database and loop it, populating each page automatically.
Here's the code that I'm working on and I'll tell you where I'm stuck:
Code: Select all
<?php
$dbc = mysqli_connect('localhost', 'username', 'password', 'database_name')
or die('Error connecting to MySQL server.');
$query = "SELECT * FROM table_name";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
while($row = mysqli_fetch_array($result))
{
$name = $row['name'];
$itemNumber = $row['itemNumber'];
$finish = $row['finish'];
$size = $row['size'];
$optionalSize = $row['optionalSize'];
$inStockLocationOne = $row['inStockLocationOne'];
$inStockLocationTwo = $row['inStockLocationTwo'];
$inStockLocationThree = $row['inStockLocationThree'];[/b]
if($inStockLocationOne == '0'){
echo "$name ... $itemNumber ... $finish ... $size $optionalSize ... $inStockCharlotte ... $inStockNewOrleans" . '<br />'; //This is for testing only
}
}
mysqli_close($dbc);
?>
Now basically what I thought of doing was entering the information into the database into the name fields above, then enter the exported variable names into inStockLocationOne, inStockLocationTwo, and inStockLocationThree.
Then I was going to call upon the variable in the "if" statement, but basically what I end up doing is saying if($inStockLocationOne == '0') which when translated of course the computer will go in and see that the $inStockLocationOne actually is assigned $item1 so it'll never generate the code.
OK FINALLY, my question is: Is there a way to call a variable that's stored in a database? So that when I type in if($inStockLocationOne == '0'), the computer when running the script will turn it into if($item1 == '0')
Re: Storing Variables in Database
Posted: Sat Sep 18, 2010 7:31 pm
by mecha_godzilla
Hi,
I've read through this a couple of times but I can't quite tell what it is that you want to do.
How do $item1, $item2, $item3, etc. get associated with records in the database? Is the question that when you're looping through all the records, you check
Code: Select all
if ($inStockLocationOne == 0) {
if ($item1 == 0) {
// do something
}
}
but you need someway to change the name of the $item1 variable to $item2, $item3, $item4, etc. based on the record's ID in the database? If so, you could do it like this:
Code: Select all
$item1 = 'yes';
$item2 = 'no';
$item3 = 'maybe';
for ($i = 1; $i<= 3; $i++) {
echo ${"item$i"};
}
HTH,
Mecha Godzilla
Re: Storing Variables in Database
Posted: Sat Sep 18, 2010 8:59 pm
by califdon
Yeh, Dino, welcome to the forums, but your terminology is so completely foreign to what we can understand, I don't know how much we can help you. I don't think I understood even ONE thing that you tried to explain. Variables are never stored in a database, VALUES are stored in a database. The values are usually represented as variables within a program in order to store them in a database, but this really has nothing to do with anything I could try to figure out from your description. You might want to try to ask it again, but if you're struggling that hard to explain yourself, we may not be able to offer much help, simply because we can't understand what you need.
Re: Storing Variables in Database
Posted: Mon Sep 20, 2010 7:50 am
by dinocajic
OK lets see if I can explain this more simply...I didn't want to go into extreme detail before.
1. The guy that worked here before actually requested for a way to link our warehouse inventory program to the website.
2. They designed an export button that takes all of the inventory in stock and converts it into variables.
- So lets say that we have 14 items in stock of item1, 10 items in stock of item2.
$item1="14";
$item2="10";
- This gets exported into a text document (.txt) and then converted to php by adding the <?php ?> tags and saving it as a .php document
3. This document then gets uploaded to the website and all the other pages feed off of that page.
------------------------------------------------------------------------------------------------------------------------------------------------------------------
This is what I want to do
4. There is no way to export a .csv file, or any other file of the inventory except that text document.
- Technically we could call the programming company and have them create a new export function, but it would cost way too much money, and way too much time.
5. I want to make the website database driven, instead of me going through the site and adding each item individually to certain pages.
6. I want to create the database, and a table within that database with a column named itemNumber
7. Then I want to store a value inside that itemNumber: "$item1"
8. Then create a php script that calls upon that table column and stores it in a variable inside the php script : $variableInPHPscript = $row['itemNumber'];
9. If I was to write <?php echo '$variableInPHPscript' ?> it would display $item1
10. But what I need it to do, and this is where I'm stuck, is for when I say <?php echo '$variableInPHPscript' ?> for it to display '14' (since $item1='14'; as mentioned in Step 2)
Is there any way to do this?
Re: Storing Variables in Database
Posted: Mon Sep 20, 2010 11:13 am
by mikosiko
what kind of program/database is your warehouse inventory?... maybe is something that you can read directly instead of create that text file?
If you are forced to work with that text file... I will load it in a table with 2 fields.. one identifying the item and other with the amount in stock, after that should be only a matter of query the DB and display the item description and the amount on hand.... or is a more special functionality that need to be programmed?
like
Code: Select all
$item = $row['itemNumber'];
$stock = $row['stock'];
echo "Item : " . $item . " Stock = ". $stock;
others options could be:
-Store the pair "itemName", "ItemValue" as an array and procces it accordingly
examples here :
http://php.net/manual/en/language.types.array.php
- Variable variables came to my mind too... but looks to me that the previous 2 options could solve it.
This document then gets uploaded to the website and all the other pages feed off of that page
should be interesting if you explain this a little better... maybe it could be implemented in a different way using the values stored in the DB directly.
Re: Storing Variables in Database
Posted: Mon Sep 20, 2010 12:45 pm
by califdon
It's quite understandable that you don't happen to have any experience with databases and web programming, that's nothing to be ashamed of. But it's also a fact that it takes a highly experienced person, probably with years of direct experience, to even BEGIN to do what you want. I would say that there is ZERO chance that you will be able to do this yourself within the next year or two. Is it possible to do this? Of course it is, and it should not be a major project for a qualified web developer. Can we tell you how to do it in this forum? No.
To try to help you explain what you want to someone who understands systems like yours, let me suggest how you should describe your objective. If I have misunderstood parts of your explanation, modify them appropriately, but for heaven's sake, drop all the discussion of variables and links and buttons! Those are programming details that mean nothing when you are determining how to solve the problem (and you are using them incorrectly, anyway).
You have a proprietary database that maintains inventory information. You also have a website where you want to display inventory levels of items as they change in your database. You need a professionally developed solution to reflect the information in the database on your website. That's it. There are a whole bunch of issues to be resolved, but that's a job for somebody who has done this a hundred times before, not for an amateur.
I don't mean to be insulting. I'm sure I don't know how to do your job, either. But you are describing a fairly complex job that requires someone who knows what he is doing. This is your company's reputation at stake. It's not a job for a hobbyist.
Re: Storing Variables in Database
Posted: Mon Sep 20, 2010 5:06 pm
by dinocajic
No need to criticize...I do know web programming (PHP, JavaScript) and I have 7 years worth of experience, I just NEVER faced an issue like this...I might have learned about this years ago, but didn't commit it to memory.
BTW, the code that I wrote for the company is far too complex to post...so I tried dumming it down so that people (who might or might not have had a similar problem) would be able to help me out, or at least say, there's no way to accomplish what you're trying to do...
Also, I've talked to my company and I've told them that I will redesigned the software that they purchased and make it completely web based...which will solve my problem, so there isn't even a point to answer my question any more, other than just for general knowledge.
p.s. It's jackasses like you that kill forums in general. I hesitated even posting a question online, cause I knew there would be the douche of the place saying "You don't know programming"
For all the other people who were beyond kind to help me out, this message by no means applies to you, and thank you for trying.
Re: Storing Variables in Database
Posted: Tue Sep 21, 2010 6:16 pm
by mecha_godzilla
Here's some code that should do what you want:
Code: Select all
$item1 = '55';
$row = array();
$row['itemNumber'] = '$item1';
$variableInPHPscript = $row['itemNumber'];
echo $variableInPHPscript . '<br />'; // should say '$item1' at this point
eval('$variableInPHPscript = "' . str_replace('"', '\\"', $variableInPHPscript) . '";');
echo $variableInPHPscript . '<br />'; // should say '55' if you're lucky
Essentially what you're doing is 'expanding' the variable. With your own script, just make sure you check whether you're using single (') or double (") quotes in the right context. There might be a smarter/simpler way to do this of course.
Just on the other comments - I don't really want to wade into the fray about who should be telling who what or what not to do - both points are valid, but the real issue is that people
are tasked to do things that they don't necessarily have the experience to carry out successfully on their first time attempt. This is usually the company's fault - they either can't or won't pay for something to be done "professionally", therefore the job falls to the person within the company that looks like they have the best chance of completing it. I wouldn't have gained the experience I have without having been "thrown in at the deep end" on numerous occasions - you either rise to the challenge (and get the job done) or fail miserably and the job gets handed to someone else.
So (and I'm not pointing at anyone here) let's keep things constructive. I'm sure you're all aware that PHP has a terrible rep in the wider programming community because of its proponent's bad attitudes
M_G