vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   What way could I do this (https://vborg.vbsupport.ru/showthread.php?t=74856)

TheMayhem 01-22-2005 03:18 AM

What way could I do this
 
I was just wondering I have all of these arrays such as $user[name]; Now what my problem is I have a repeating loop that gets a few templates that I store in the database. Don't ask why I just want them their for what I'm trying to learn. My problem is whenever I loop and echo, some of those templates will call upon stuff like $user[name] and it should output a text of like Joe for example... instead it only ouputs the actual text of $user[name].

Hopefully I'm not confusing anyone, but is there a way that if you define something in a file and then run a query if the query has any information on the echo statement of whats stored inside such as an array or variable that on the output it would display what it has been defined too?

Andreas 01-22-2005 03:21 AM

You muste evaluate templates to get the variables in:

PHP Code:

eval('$template = "' fetch_template('templatenamer') . '";'); 


noppid 01-22-2005 03:23 AM

Look at the code at the top of every vB file, see a pattern? Especially this...

PHP Code:

require_once('./global.php'); 

$bbuserinfo comes from there and is an array with all of the current user's vb info if I understand correctly. I do not have a list of what's in there. There may be one at vb.com.

yeah, and what he said too.

TheMayhem 01-22-2005 03:27 AM

So lets say I had something like this...

PHP Code:

$result mysql_query('SELECT * FROM templates'); 
while (
$row mysql_fetch_assoc($result)) 

    
$templates[$row['name']] = $row['template'];     


echo 
$templates['header']; 
echo 
$templates['index']; 

How would I complete the transformation? Btw noppid this isn't for a vb, I'm just trying to learn PHP by creating a complex news program

Andreas 01-22-2005 03:40 AM

PHP Code:

$compiled_header_template str_replace('\\\\$''\\$'addslashes($templates['header']));
eval(
'$header = "' $compiled_header_template '";');
echo 
$header


TheMayhem 01-22-2005 04:08 AM

There's a few problems that arise when I do that... It now retrieves the data but at the same time when it does, it now won't let me do like set widths for tables or to be able to call upon any CSS such as the class = to whatever and so forth... Any ways around that one?

Andreas 01-22-2005 04:12 AM

Hmm ...

PHP Code:

<?php
 $foobar 
'Foo Bar';
 
$templates['header'] = '<table width="100%" class="mytable"><tr><td>$foobar</td></tr></table>';
 
$compiled_header_template str_replace('\\\\$''\\$'addslashes($templates['header']));
 eval(
'$header = "' $compiled_header_template '";');
 echo 
$header
?>

Output
HTML Code:

<table width="100%" class="mytable"><tr><td>Foo Bar</td></tr></table>
Seems to work for me.

TheMayhem 01-22-2005 02:06 PM

I've triple checked this and something is definately wrong with this... This is my entire php code for anything retaining towards the templates.

PHP Code:

$result mysql_query('SELECT * FROM templates'); 
while (
$row mysql_fetch_assoc($result)) 

    
$templates[$row['name']] = $row['template'];     



// echo $templates['header']; 


$compiled_header_template str_replace('\\\\$''\\$'addslashes($templates['header'])); 
eval(
'$header = "' $compiled_header_template '";'); 
echo 
$header

If I just echo the $header template without doing any of the str_replace functions and so forth.... The css will work, the set table widths, and so forth will work, BUT any variables or arrays that I output wont. I can use your way and get most of the variables and arrays to ouput but then it will not accept the CSS properly nor the set table widths, heights, and so forth. Any idea on what could possibly be the problem?

Andreas 01-22-2005 02:09 PM

The code seems to be OK.
Can you post a template that does not work?

TheMayhem 01-22-2005 02:26 PM

PHP Code:

<body bgcolor='#ffffff' leftmargin='0' topmargin='0' bottommargin='0' rightmargin='0'>
<
table cellpadding='0' cellspacing='0' border='0' class='maintable' width='650' align='center'>
<
tr>
<
td>LOGO GOES HERE</td>
</
tr>
<
tr>
<
td background='images/navbg.jpg' height='20'>LINKS</td>
</
tr>
<
tr>
<
td

This is the header template that doesnt even call upon any arrays. I have it after all the php code at the bottom of the page right before the last } ?> and even this one will not cooperate

Andreas 01-22-2005 02:48 PM

Try this
PHP Code:

$compiled_header_template str_replace(array('\\\\$''\\\''), array('\\$''\''), addslashes($templates['header'])); 

Haven't thought of using ' for tag arguments, as I always use " :)

Zachery 01-22-2005 02:54 PM

Quote:

Originally Posted by TheMayhem
PHP Code:

<body bgcolor='#ffffff' leftmargin='0' topmargin='0' bottommargin='0' rightmargin='0'>
<
table cellpadding='0' cellspacing='0' border='0' class='maintable' width='650' align='center'>
<
tr>
<
td>LOGO GOES HERE</td>
</
tr>
<
tr>
<
td background='images/navbg.jpg' height='20'>LINKS</td>
</
tr>
<
tr>
<
td

This is the header template that doesnt even call upon any arrays. I have it after all the php code at the bottom of the page right before the last } ?> and even this one will not cooperate

Your problem is bad html... nothing more

in HTML all attribute varibles should be nested inside of DOUBLE quotes, not SINGLE


HTML Code:

<body bgcolor="#ffffff" leftmargin="0" topmargin="0" bottommargin="0" rightmargin="0">
<table cellpadding="0" cellspacing="0" border="0" class="maintable" width="650" align="center">
<tr>
<td>LOGO GOES HERE</td>
</tr>
<tr>
<td background="images/navbg.jpg" height="20">LINKS</td>
</tr>
<tr>
<td>


TheMayhem 01-22-2005 03:57 PM

Hmmm I figured if I used " then php could get an error... guess not. My last question is now for my news I want to display a loop statement, which I use a while. Now I know this won't work correctly because I'm using a mysql_fetch_array query inside a while statement and it is going to loop the number of times I have an entree in the db. So how exactly could I take a while statement that is going to loop and actually store the information in the template so it shows each and every loop, such as $news[news_name] should loop 8 times and thus loops 8 times in the template and shoes each array being different then the last?

Andreas 01-22-2005 04:02 PM

I've read this 3 times now and still don't understand your problem :(

sabret00the 01-22-2005 04:02 PM

Quote:

Originally Posted by TheMayhem
Hmmm I figured if I used " then php could get an error... guess not.

if you're echoing out the data then you need to escape each backslash or just go back to using apostraphies, however if it's a template then theirs no issues :)

Quote:

Originally Posted by TheMayhem
Hmmm I figured if I used " then php could get an error... guess not. My last question is now for my news I want to display a loop statement, which I use a while. Now I know this won't work correctly because I'm using a mysql_fetch_array query inside a while statement and it is going to loop the number of times I have an entree in the db. So how exactly could I take a while statement that is going to loop and actually store the information in the template so it shows each and every loop, such as $news[news_name] should loop 8 times and thus loops 8 times in the template and shoes each array being different then the last?

your best bet would be to write out the code (or what you think the code would look like for what you're trying to acheive) and post it, then i'd have a better idea of what you mean.

TheMayhem 01-22-2005 04:25 PM

LoL sorry, I know you'll understand it this way.

PHP Code:

$news_query mysql_query("select * from news ORDER BY `categorys_order` ASC");
    while (
$news_showmysql_fetch_array($news_query)) {
// echo "$news_show[news_name];


Just a simple while statement. Now I can't just store it in the templates and call upon $news_show[news_name] because it won't display since this thing is going to loop several times. So how could I get it in the templates to show the results of each loop?

Andreas 01-22-2005 04:34 PM

I don't understand what you want, but I guess you want this:

PHP Code:

eval('$news .= "' $compiled_news_template '";'); 


TheMayhem 01-22-2005 04:40 PM

Nah I don't think your following me. You have a while { }. Let's say it loops 5 times and the following pieces of data it is going to pull from the same table in the database becaused you used a get array query.
John
Joe
Jerry
James
Judy

It retrieves 5 names. And in order to retrieve those 5 names all I had to type inside the while brackets were something like $news[display_names]. That is it.

Well on the template how could I get it so that it displays all 5 loops so each name is displayed because I can't write the php statement inside the template, nor can I output the echo statement inside the while loop. I know vBulletin does this when it wants to ouput like each forum's description, name, and so forth on their forumhome but vb uses a lot more coding that is far too advanced and complex for my simple brain. Any ideas?

Andreas 01-22-2005 04:49 PM

So in simple words:
You want the news_name values from all rows in the table in one string?

Before the while:
PHP Code:

$newsnames = array(); 

In the wile:
PHP Code:

$newsnames[] = $news_show['news_name']; 

After the while
PHP Code:

$newsnames implode(' '$newsnames); 


TheMayhem 01-22-2005 05:33 PM

The implode function works... but not exactly as needed. I'm trying to retrieve 3 specific bits of information. news_name, news_id, news_description. When i use the implode function I can only assign one variable and have it implode one bit of information. To make it easier to understand this is what I want it to look like when complete for the output.

<tr>
<td>$news_name</td><td>$news_id</td><td>$news_description</td>
</tr>

So it'd display the following bits of info on the output
Joe 1 This is my piece of writing
John 2 I don't like writing
Jerry 3 Wow I can't believe I did this
Jose 4 WEEEE!

Currently it'll display
JoeJohnjerryJose 1234 ect.

Basically I want to use a little simple html to seperate everything a bit and for me to only have to call upon a few variables in my templates in order to complete this process.

With the implode function I can't necessarily do that because if I call upon something like $news_name it will loop all the $news_names that I have selected. And thus on the output will look horribly ugly. Is there any function i can use so I could like insert this html and it'd select those 3... then loop again and keep looping like the implode function did until the while statement is over?

Andreas 01-22-2005 05:50 PM

Are we getting there? ^.^

Save this as template newsbit
Code:

<tr>
<td>$news_shown[news_name]</td><td>$news_shown[news_id]</td><td>$news_shown[news_description]</td>
</tr>

Before the while
PHP Code:

$compiled_newsbit_template str_replace(array('\\\\$''\\\''), array('\\$''\''), addslashes($templates['newsbits'])); 

In the while
PHP Code:

eval('$newsbits .= "' $compiled_newsbit_template '";'); 

Now place $newsbits in your main news template and you're done

AnhTuanCool 01-22-2005 06:01 PM

How can I evaluate a variable from an array (not using while...fetch_array because there is no SQL involved)?

PHP Code:

eval('$news .= "' $compiled_news_template '";'); 


Marco van Herwaarden 01-22-2005 10:07 PM

foreach instead of a while maybe.


All times are GMT. The time now is 04:50 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.01426 seconds
  • Memory Usage 1,839KB
  • 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
  • (2)bbcode_html_printable
  • (17)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (23)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