View Full Version : Noobie array help, please.
robertpro2a
04-14-2009, 09:11 PM
Hi,
This should be easy but I'm not sure where to go from here. I made a "search.php" file that queries a mySQL db and I want the result to be shown in a VB template. So, I have succesfully duplicated the vb template:
(I won't show all the code b/c you all know what it looks like, but for reference it's this I'm talking about) :
<?php
// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);
// #################### DEFINE IMPORTANT CONSTANTS #######################
define('NO_REGISTER_GLOBALS', 1);
define('THIS_SCRIPT', 'Range Locator'); // change this depending on your filename
I can make it say "HELLO WORLD!!" using the forum template.
After the above code, I connect to the database and run a query.
I have the results stored in a recordset like this:
function get_ranges($query)
{
$result = mysql_query($query);
$data = array();
$row = 0;
while ($r = mysql_fetch_assoc($result)){
foreach ($r as $key => $value){
$data[$row][$key] = $value;
}
$row++;
}
return $data;
}
How do I store that into some vb variable so I can access it in the HTML template later?
Something like this?
$array= construct_navbits($array);
Thanks.
TigerC10
04-15-2009, 01:43 AM
No, you'll do like this
$myarray = get_ranges("...");
In between the parenthesis and quotes, replace the ... with the SQL statement.
Although if you're using the vBulletin database, there's a better way of executing the query than what you're using...
Dismounted
04-15-2009, 05:15 AM
You want to use the vBulletin database class instead (as it also does MySQLi).
$array = array();
$data = $vbulletin->db->query_read("
SELECT *
FROM table
");
while ($row = $vbulletin->db->fetch_array($data))
{
$array[$row['primaryid']] = $row;
}
robertpro2a
04-16-2009, 01:59 AM
Thank you both.
Dismounted: quick question. I'm not trying to trick you into writing the code for me, but I need a bit of help, please.
My query is fine, but if you'd like to see it here it is:
//query
$query = "SELECT * From `gun_ranges` where `state` = '$state' ";
I need to retrieve 5 columns of data from that. So, using your code, would I do this?
$array = array();
$data = $vbulletin->db->query_read( $query );
while ($row = $vbulletin->db->fetch_array($data))
{
$array[$row['website']] = $row; //gun range's website
$array[$row['phone']] = $row; //gun range's phone number
//etc....
}
Is this correct? And if so, could you please point me in the direction on how I would retrieve this information via the template HTML?
Thank you.
TigerC10
04-16-2009, 02:46 AM
Man you weren't kidding when you said "noobie". You've got it backwards. Your query could be improved too...
$array = array();
$query = "SELECT * From ".TABLE_PREFIX."gun_ranges where state = '$state' ";
$data = $vbulletin->db->query_read( $query );
$count = 0;
while ($row = $vbulletin->db->fetch_array($data))
{
$array[$count]['website'] = $row['website']; //gun range website
$array[$count]['phone'] = $row['phone']; //gun range phone number
//etc....
$count+=1;
}
echo "0's Website: ". $array[0]['website'];
Alternatively...
$array = array();
$query = "SELECT * From ".TABLE_PREFIX."gun_ranges where state = '$state' ";
$data = $vbulletin->db->query_read( $query );
$count = 0;
while ($row = $vbulletin->db->fetch_array($data))
{
$array[$count] = $row; //copies everything all at once
$count+=1;
}
echo "0's Website: ". $array[0]['website'];
Dismounted
04-16-2009, 02:48 AM
$templatebits = '';
$data = $vbulletin->db->query_read("
SELECT *
FROM gun_ranges
WHERE state = '" . $vbulletin->db->escape_string($state) . "'
LIMIT 5
");
while ($row = $vbulletin->db->fetch_array($data))
{
eval('$templatebits .= "' . fetch_template('templatebit') . '";');
}
TigerC10
04-16-2009, 02:52 AM
Dismounted, do you really think that $state is a user inputted variable?
Dismounted
04-16-2009, 03:05 AM
User-input or not, it needs to be cleaned beforehand.
Hypothetical: Assume it was added by Admins through the Admin CP. Unknowing Admin uses single quotes. Bam! Your query fails.
TigerC10
04-16-2009, 03:12 AM
True, but this guy is making a $query variable instead of inlining the string. I think he's done the same thing with something in the $state variable.
But I guess it's better to be safe than sorry. :)
robertpro2a
04-16-2009, 05:18 AM
Hi guys,
Well, my SQL is fine since I actually did sql injection prevention before the query..but in regards to the other codes, they are causing the php page to turn blank, which means there is an error...
Tiger: I think the reason why yours is doing it is because you can't echo within the php file, since its meant to display the template.
Dismounted: I'm not sure why yours isn't working either.
...still grateful for the help so far!!
Dismounted
04-16-2009, 05:31 AM
Did you create and (edit the code to) use the appropriate templates?
robertpro2a
04-16-2009, 05:54 AM
Ooops, I forgot to change it thanks. But what doesn't make sense to me is, in the above loop you made, it is not doing anything to assign the rows to the variable.
while ($row = $vbulletin->db->fetch_array($data))
{
eval('$templatebits .= "' . fetch_template('range_results') . '";');
//this is just fetching the template...
}
I've tested it also by putting $templatebits in the HTML template and nothing appears.
In the style manager:
"range_results"
$stylevar[htmldoctype]
<html dir="$stylevar[textdirection]" lang="$stylevar[languagecode]">
<head>
<title>$vboptions[bbtitle]</title>
$headinclude
</head>
<body>
results:
$templatebits
$navbar
Dismounted
04-16-2009, 06:34 AM
range_resultbit
<br />Title: $row[title]
range_results
$stylevar[htmldoctype]
<html dir="$stylevar[textdirection]" lang="$stylevar[languagecode]">
<head>
<title>$vboptions[bbtitle]</title>
$headinclude
</head>
<body>
$header
$navbar
<p>Results:
$templatebits</p>
$footer
</body>
</html>
$templatebits = '';
$data = $vbulletin->db->query_read("
SELECT *
FROM gun_ranges
WHERE state = '" . $vbulletin->db->escape_string($state) . "'
LIMIT 5
");
while ($row = $vbulletin->db->fetch_array($data))
{
eval('$templatebits .= "' . fetch_template('range_resultbit') . '";');
}
eval('print_output("' . fetch_template('range_results') . '");');
TigerC10
04-16-2009, 02:15 PM
Tiger: I think the reason why yours is doing it is because you can't echo within the php file, since its meant to display the template.
1) I just gave the echo code to show you how to access the var.
2) You can echo anything within a php file. The echo command is the same as print.
robertpro2a
04-16-2009, 05:34 PM
range_resultbit
<br />Title: $row[title]
range_results
$stylevar[htmldoctype]
<html dir="$stylevar[textdirection]" lang="$stylevar[languagecode]">
<head>
<title>$vboptions[bbtitle]</title>
$headinclude
</head>
<body>
$header
$navbar
<p>Results:
$templatebits</p>
$footer
</body>
</html>
$templatebits = '';
$data = $vbulletin->db->query_read("
SELECT *
FROM gun_ranges
WHERE state = '" . $vbulletin->db->escape_string($state) . "'
LIMIT 5
");
while ($row = $vbulletin->db->fetch_array($data))
{
eval('$templatebits .= "' . fetch_template('range_resultbit') . '";');
}
eval('print_output("' . fetch_template('range_results') . '");');
YES!!! It works. Thanks! I owe you a beer if you're ever in the Baltimore area.
Tiger: You too buddy!
vBulletin® v3.8.12 by vBS, Copyright ©2000-2024, vBulletin Solutions Inc.