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

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.

PHP Code:

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"

PHP Code:

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

<br />Title: $row[title]
range_results
Code:

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

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

eval(
'print_output("' fetch_template('range_results') . '");'); 


TigerC10 04-16-2009 02:15 PM

Quote:

Originally Posted by robertpro2a (Post 1792226)
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

Quote:

Originally Posted by Dismounted (Post 1792253)
range_resultbit
Code:

<br />Title: $row[title]
range_results
Code:

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

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


All times are GMT. The time now is 08:37 PM.

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.01463 seconds
  • Memory Usage 1,812KB
  • 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
  • (5)bbcode_code_printable
  • (12)bbcode_php_printable
  • (2)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (15)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
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete