vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Noobie array help, please. (https://vborg.vbsupport.ru/showthread.php?t=211226)

robertpro2a 04-14-2009 09:11 PM

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.

TigerC10 04-15-2009 01:43 AM

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...

Dismounted 04-15-2009 05:15 AM

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;



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:
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.

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...

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']; 


Dismounted 04-16-2009 02:48 AM

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') . '";');



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!!


All times are GMT. The time now is 08:58 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.02070 seconds
  • Memory Usage 1,767KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (8)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete