查询以显示包含所有附加缩略图的帖子

时间:2017-04-27 12:12:24

标签: php mysql

问题: 我需要显示一个帖子列表,每个帖子都带有附加的缩略图。 缩略图在文件系统中,我只需要存储在mysql数据库中的路径。 我有2个数据库表:一个用于posts,另一个用于images_attachments

我不确定我应该从db中检索哪种格式以实现我需要的格式。 现在我有这个:

SELECT posts.id, posts.post_title, images_attachments.image_name
FROM posts 
INNER JOIN images_attachments ON posts.id = image_post_post_id

这会生成包含post_idpost_title和相应联接images_attachments.image_name的行。

这是我应该怎么做的?问题是我每个帖子的行数与图像数量相同。这将在模板上包含更多逻辑以显示帖子标题,并在其下面显示所有拇指。

第二个问题是它会忽略没有附加缩略图的帖子。

请告知我是否方向错误,如果是这样,请如何格式化查询。

(这不是wordpress,只是原始的mysql和php模板)

1 个答案:

答案 0 :(得分:1)

你有多对一(一个帖子的许多附件),因此你可能更好的做一个正常的选择,循环遍历行,并查询附件。

# Source: https://blog.keras.io/building-autoencoders-in-keras.html
import numpy as np
np.random.seed(2713)

from keras.layers import Input, Dense
from keras.models import Model
from keras import regularizers

encoding_dim = 32

input_img = Input(shape=(784,))
# add a Dense layer with a L1 activity regularizer
encoded = Dense(encoding_dim, activation='relu',
                activity_regularizer=regularizers.l1(10e-5))(input_img)
decoded = Dense(784, activation='sigmoid')(encoded)

autoencoder = Model(input_img, decoded)

# this model maps an input to its encoded representation
encoder = Model(input_img, encoded)

# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))

autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

from keras.datasets import mnist
(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
print(x_train.shape)
print(x_test.shape)

autoencoder.fit(x_train, x_train,
                epochs=100,
                batch_size=256,
                shuffle=True,
                validation_data=(x_test, x_test))

# encode and decode some digits
# note that we take them from the *test* set
encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)

# use Matplotlib (don't ask)
import matplotlib.pyplot as plt

n = 10  # how many digits we will display
plt.figure(figsize=(20, 4))
for i in range(n):
    # display original
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(x_test[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # display reconstruction
    ax = plt.subplot(2, n, i + 1 + n)
    plt.imshow(decoded_imgs[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()

您的方式,您需要根据附件的数量创建动态的附加列,这是一个令人头疼的问题。这也可以确保您的帖子没有附件。