Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 04-14-2009, 09:11 PM
robertpro2a robertpro2a is offline
 
Join Date: Mar 2008
Posts: 55
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Noobie array help, please.

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 Code:
<?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:


PHP Code:
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.
Reply With Quote
  #2  
Old 04-15-2009, 01:43 AM
TigerC10's Avatar
TigerC10 TigerC10 is offline
 
Join Date: Apr 2006
Location: Austin, TX
Posts: 616
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

No, you'll do like this

Code:
$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...
Reply With Quote
  #3  
Old 04-15-2009, 05:15 AM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You want to use the vBulletin database class instead (as it also does MySQLi).
PHP Code:
$array = array();
$data $vbulletin->db->query_read("
    SELECT *
    FROM table
"
);

while (
$row $vbulletin->db->fetch_array($data))
{
    
$array[$row['primaryid']] = $row;

Reply With Quote
  #4  
Old 04-16-2009, 01:59 AM
robertpro2a robertpro2a is offline
 
Join Date: Mar 2008
Posts: 55
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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:
PHP Code:
//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?

PHP Code:
 $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.
Reply With Quote
  #5  
Old 04-16-2009, 02:46 AM
TigerC10's Avatar
TigerC10 TigerC10 is offline
 
Join Date: Apr 2006
Location: Austin, TX
Posts: 616
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Man you weren't kidding when you said "noobie". You've got it backwards. Your query could be improved too...

PHP Code:
$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...

PHP Code:
$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']; 
Reply With Quote
  #6  
Old 04-16-2009, 02:48 AM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
$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') . '";');

Reply With Quote
  #7  
Old 04-16-2009, 02:52 AM
TigerC10's Avatar
TigerC10 TigerC10 is offline
 
Join Date: Apr 2006
Location: Austin, TX
Posts: 616
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Dismounted, do you really think that $state is a user inputted variable?
Reply With Quote
  #8  
Old 04-16-2009, 03:05 AM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #9  
Old 04-16-2009, 03:12 AM
TigerC10's Avatar
TigerC10 TigerC10 is offline
 
Join Date: Apr 2006
Location: Austin, TX
Posts: 616
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #10  
Old 04-16-2009, 05:18 AM
robertpro2a robertpro2a is offline
 
Join Date: Mar 2008
Posts: 55
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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!!
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:30 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04461 seconds
  • Memory Usage 2,292KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (8)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete