bigheadedd Posted March 15, 2012 Share Posted March 15, 2012 Hi, I'm wondering if theres a shortcut to a potential problem I have. I'm currently running a query on my website to pull all the fields from a table in my database, for the data to be used on various parts of the page. Usually I would do something as follows $result = mysql_query("SELECT * FROM table WHERE page='1'"); while ($row=mysql_fetch_array($result)) { $title = $row["title"]; $data = $row["data"]; } And so on and so forth. I would then call the appropriate data by echoing $data for example. However, my table contains a lot more rows than i've mentioned (Around 25 or so). Rather than assigning each to a variable and having a large portion of variable assignments at the top of the page, is there any clever way of putting all of these values inside of an array. So for example, I could call $array_data["title"] or $array_data["data"]?? So it keeps the same key, but puts it inside of an array that I don't have to loop through each time? Hope that makes sense! Thanks, Edd Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted March 15, 2012 Share Posted March 15, 2012 That would actually add an extra step and require much more code. Loops are your friend. Quote Link to comment Share on other sites More sharing options...
bigheadedd Posted March 15, 2012 Author Share Posted March 15, 2012 Really? At the moment, I call $row["data"] for example, but it has to be within a mysql_fetch_array() function/loop. What i'm wondering, is that rather than looping through, resetting the query to the beginning etc each time, be able to put it inside of an array that functions the same as $row["data"], but not within the mysql loop?? I think that makes sense..? Quote Link to comment Share on other sites More sharing options...
dmikester1 Posted March 15, 2012 Share Posted March 15, 2012 You have 25 rows of data or 25 fields(columns)? Can you give a little more description of what exactly you are trying to do? Quote Link to comment Share on other sites More sharing options...
billkirim Posted March 15, 2012 Share Posted March 15, 2012 haha.. i was writing the same question right now Quote Link to comment Share on other sites More sharing options...
bigheadedd Posted March 15, 2012 Author Share Posted March 15, 2012 Hi, Ah yes, sorry! I've actually figured out a way of doing what I wanted to do. Rather than doing multiple while loops using mysql_fetch_array, or avoiding using a large one at the beginning (and assigning all of the information to variables), I wanted to put it all into an array with the same keys so I could call it without using loops. The code I came up with is: while ($data[]=mysql_fetch_assoc($tresult)); echo $data[0]["title"]; echo $data[0]["information"]; etc etc. I've realised this is what I needed, but never tried it before. Of course, if theres a better way, let me know Thanks! Quote Link to comment Share on other sites More sharing options...
dmikester1 Posted March 15, 2012 Share Posted March 15, 2012 Do you want everything to be stored at $data[0]? If not, I suggest sticking a variable in there like this: $i = 0; while ($data[]=mysql_fetch_assoc($tresult)); echo $data[$i]["title"]; echo $data[$i]["information"]; $i++; Quote Link to comment Share on other sites More sharing options...
bigheadedd Posted March 15, 2012 Author Share Posted March 15, 2012 I did think about that actually yes. In my query I have left joined another table, so in some cases it will be more than just 0. I realised though that I can just loop through this initial loop to get the subset of data. Thanks for the help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.